1+ import __main__
12import time
23import heapq
4+ import errno
5+ import logging
36
47
8+ log = logging .getLogger ("asyncio" )
9+
510def coroutine (f ):
611 return f
712
@@ -24,7 +29,7 @@ def call_later(self, delay, callback, *args):
2429 def call_at (self , time , callback , * args ):
2530# self.q.append((callback, args))
2631 # self.cnt is workaround per heapq docs
27- # print ("Scheduling", (time, self.cnt, callback, args))
32+ log . debug ("Scheduling %s " , (time , self .cnt , callback , args ))
2833 heapq .heappush (self .q , (time , self .cnt , callback , args ))
2934# print(self.q)
3035 self .cnt += 1
@@ -42,6 +47,8 @@ def run_forever(self):
4247 while True :
4348 if self .q :
4449 t , cnt , cb , args = heapq .heappop (self .q )
50+ log .debug ("Next task to run: %s" , (t , cnt , cb , args ))
51+ # __main__.mem_info()
4552 tnow = self .time ()
4653 delay = t - tnow
4754 if delay > 0 :
@@ -57,20 +64,22 @@ def run_forever(self):
5764 try :
5865 if args == ():
5966 args = (None ,)
60- print ( "Send args:" , args )
67+ log . debug ( "Gen send args: %s " , args )
6168 ret = cb .send (* args )
62- print ( "ret: " , ret )
69+ log . debug ( "Gen yield result: %s " , ret )
6370 if isinstance (ret , SysCall ):
6471 if isinstance (ret , Sleep ):
6572 delay = ret .args [0 ]
6673 elif isinstance (ret , IORead ):
74+ # self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
75+ # self.add_reader(ret.obj.fileno(), lambda c, f: self.call_soon(c, f), cb, ret.obj)
6776 self .add_reader (ret .obj .fileno (), lambda f : self .call_soon (cb , f ), ret .obj )
6877 continue
6978 elif isinstance (ret , IOWrite ):
7079 self .add_writer (ret .obj .fileno (), lambda f : self .call_soon (cb , f ), ret .obj )
7180 continue
7281 except StopIteration as e :
73- print ( cb , " finished" )
82+ log . debug ( "Gen finished: %s" , cb )
7483 continue
7584 #self.q.append(c)
7685 self .call_later (delay , cb , * args )
@@ -99,22 +108,22 @@ def __init__(self):
99108 self .poller = select .epoll (1 )
100109
101110 def add_reader (self , fd , cb , * args ):
102- print ("add_reader" )
111+ log . debug ("add_reader%s" , ( fd , cb , args ) )
103112 self .poller .register (fd , select .EPOLLIN , (cb , args ))
104113
105114 def add_writer (self , fd , cb , * args ):
106- print ("add_writer" )
115+ log . debug ("add_writer%s" , ( fd , cb , args ) )
107116 self .poller .register (fd , select .EPOLLOUT , (cb , args ))
108117
109118 def wait (self , delay ):
110- print ("epoll.wait" , delay )
119+ log . debug ("epoll.wait(%d) " , delay )
111120 if delay == - 1 :
112121 res = self .poller .poll (- 1 )
113122 else :
114123 res = self .poller .poll (int (delay * 1000 ))
115- print ( "poll: " , res )
124+ log . debug ( "epoll result: %s " , res )
116125 for cb , ev in res :
117- print ("Calling %s%s" % ( cb [0 ], cb [1 ]) )
126+ log . debug ("Calling IO callback: %s%s" , cb [0 ], cb [1 ])
118127 cb [0 ](* cb [1 ])
119128
120129
@@ -164,11 +173,11 @@ def __init__(self, s):
164173 self .s = s
165174
166175 def readline (self ):
167- print ( " readline" )
176+ log . debug ( "StreamReader. readline() " )
168177 s = yield IORead (self .s )
169- print ( " after IORead" )
178+ log . debug ( "StreamReader.readline(): after IORead: %s" , s )
170179 res = self .s .readline ()
171- print ( " readline res:" , res )
180+ log . debug ( "StreamReader. readline(): res: %s " , res )
172181 return res
173182
174183
@@ -186,15 +195,17 @@ def write(self, buf):
186195
187196
188197def open_connection (host , port ):
198+ log .debug ("open_connection(%s, %s)" , host , port )
189199 s = _socket .socket ()
190200 s .setblocking (False )
191201 ai = _socket .getaddrinfo (host , port )
192202 addr = ai [0 ][4 ]
193203 try :
194204 s .connect (addr )
195205 except OSError as e :
196- print (e .args [0 ])
197- print ("After connect" )
206+ if e .args [0 ] != errno .EINPROGRESS :
207+ raise
208+ log .debug ("open_connection: After connect" )
198209 s = yield IOWrite (s )
199- print ( " After iowait:" , s )
210+ log . debug ( "open_connection: After iowait: %s " , s )
200211 return StreamReader (s ), StreamWriter (s )
0 commit comments