77
88log = logging .getLogger ("asyncio" )
99
10- IO_READ = 1
11- IO_WRITE = 2
12-
1310type_gen = type ((lambda : (yield ))())
1411
1512class EventLoop :
@@ -65,21 +62,21 @@ def run_forever(self):
6562 ret = cb .send (* args )
6663 log .debug ("Coroutine %s yield result: %s" , cb , ret )
6764 if isinstance (ret , SysCall ):
65+ arg = ret .args [0 ]
6866 if isinstance (ret , Sleep ):
69- delay = ret . args [ 0 ]
67+ delay = arg
7068 elif isinstance (ret , IORead ):
7169# self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
7270# 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 )
7472 continue
7573 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 )
7775 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 ())
8380 elif isinstance (ret , type_gen ):
8481 self .call_soon (ret )
8582 elif ret is None :
@@ -146,8 +143,7 @@ def wait(self, delay):
146143
147144class SysCall :
148145
149- def __init__ (self , call , * args ):
150- self .call = call
146+ def __init__ (self , * args ):
151147 self .args = args
152148
153149 def handle (self ):
@@ -157,20 +153,16 @@ class Sleep(SysCall):
157153 pass
158154
159155class IORead (SysCall ):
160-
161- def __init__ (self , obj ):
162- self .obj = obj
156+ pass
163157
164158class IOWrite (SysCall ):
159+ pass
165160
166- def __init__ (self , obj ):
167- self .obj = obj
168-
169- class IODone (SysCall ):
161+ class IOReadDone (SysCall ):
162+ pass
170163
171- def __init__ (self , op , obj ):
172- self .op = op
173- self .obj = obj
164+ class IOWriteDone (SysCall ):
165+ pass
174166
175167
176168def get_event_loop ():
@@ -184,7 +176,7 @@ def async(coro):
184176 return coro
185177
186178def sleep (secs ):
187- yield Sleep ("sleep" , secs )
179+ yield Sleep (secs )
188180
189181
190182import microsocket as _socket
@@ -202,7 +194,7 @@ def read(self, n=-1):
202194 break
203195 log .warn ("Empty read" )
204196 if not res :
205- yield IODone ( IO_READ , self .s )
197+ yield IOReadDone ( self .s )
206198 return res
207199
208200 def readline (self ):
@@ -215,7 +207,7 @@ def readline(self):
215207 break
216208 log .warn ("Empty read" )
217209 if not res :
218- yield IODone ( IO_READ , self .s )
210+ yield IOReadDone ( self .s )
219211 log .debug ("StreamReader.readline(): res: %s" , res )
220212 return res
221213
@@ -247,7 +239,7 @@ def awrite(self, buf):
247239 log .debug ("StreamWriter.awrite(): can write more" )
248240
249241 def close (self ):
250- yield IODone ( IO_WRITE , self .s )
242+ yield IOWriteDone ( self .s )
251243 self .s .close ()
252244
253245
0 commit comments