7
7
8
8
log = logging .getLogger ("asyncio" )
9
9
10
- IO_READ = 1
11
- IO_WRITE = 2
12
-
13
10
type_gen = type ((lambda : (yield ))())
14
11
15
12
class EventLoop :
@@ -65,21 +62,21 @@ def run_forever(self):
65
62
ret = cb .send (* args )
66
63
log .debug ("Coroutine %s yield result: %s" , cb , ret )
67
64
if isinstance (ret , SysCall ):
65
+ arg = ret .args [0 ]
68
66
if isinstance (ret , Sleep ):
69
- delay = ret . args [ 0 ]
67
+ delay = arg
70
68
elif isinstance (ret , IORead ):
71
69
# self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
72
70
# self.add_reader(ret.obj.fileno(), lambda c, f: self.call_soon(c, f), cb, ret.obj)
73
- self .add_reader (ret . obj . fileno (), lambda cb , f : self .call_soon (cb , f ), cb , ret . obj )
71
+ self .add_reader (arg . fileno (), lambda cb , f : self .call_soon (cb , f ), cb , arg )
74
72
continue
75
73
elif isinstance (ret , IOWrite ):
76
- self .add_writer (ret . obj . fileno (), lambda cb , f : self .call_soon (cb , f ), cb , ret . obj )
74
+ self .add_writer (arg . fileno (), lambda cb , f : self .call_soon (cb , f ), cb , arg )
77
75
continue
78
- elif isinstance (ret , IODone ):
79
- if ret .op == IO_READ :
80
- self .remove_reader (ret .obj .fileno ())
81
- elif ret .op == IO_WRITE :
82
- self .remove_writer (ret .obj .fileno ())
76
+ elif isinstance (ret , IOReadDone ):
77
+ self .remove_reader (arg .fileno ())
78
+ elif isinstance (ret , IOWriteDone ):
79
+ self .remove_writer (arg .fileno ())
83
80
elif isinstance (ret , type_gen ):
84
81
self .call_soon (ret )
85
82
elif ret is None :
@@ -146,8 +143,7 @@ def wait(self, delay):
146
143
147
144
class SysCall :
148
145
149
- def __init__ (self , call , * args ):
150
- self .call = call
146
+ def __init__ (self , * args ):
151
147
self .args = args
152
148
153
149
def handle (self ):
@@ -157,20 +153,16 @@ class Sleep(SysCall):
157
153
pass
158
154
159
155
class IORead (SysCall ):
160
-
161
- def __init__ (self , obj ):
162
- self .obj = obj
156
+ pass
163
157
164
158
class IOWrite (SysCall ):
159
+ pass
165
160
166
- def __init__ (self , obj ):
167
- self .obj = obj
168
-
169
- class IODone (SysCall ):
161
+ class IOReadDone (SysCall ):
162
+ pass
170
163
171
- def __init__ (self , op , obj ):
172
- self .op = op
173
- self .obj = obj
164
+ class IOWriteDone (SysCall ):
165
+ pass
174
166
175
167
176
168
def get_event_loop ():
@@ -184,7 +176,7 @@ def async(coro):
184
176
return coro
185
177
186
178
def sleep (secs ):
187
- yield Sleep ("sleep" , secs )
179
+ yield Sleep (secs )
188
180
189
181
190
182
import microsocket as _socket
@@ -202,7 +194,7 @@ def read(self, n=-1):
202
194
break
203
195
log .warn ("Empty read" )
204
196
if not res :
205
- yield IODone ( IO_READ , self .s )
197
+ yield IOReadDone ( self .s )
206
198
return res
207
199
208
200
def readline (self ):
@@ -215,7 +207,7 @@ def readline(self):
215
207
break
216
208
log .warn ("Empty read" )
217
209
if not res :
218
- yield IODone ( IO_READ , self .s )
210
+ yield IOReadDone ( self .s )
219
211
log .debug ("StreamReader.readline(): res: %s" , res )
220
212
return res
221
213
@@ -247,7 +239,7 @@ def awrite(self, buf):
247
239
log .debug ("StreamWriter.awrite(): can write more" )
248
240
249
241
def close (self ):
250
- yield IODone ( IO_WRITE , self .s )
242
+ yield IOWriteDone ( self .s )
251
243
self .s .close ()
252
244
253
245
0 commit comments