@@ -15,6 +15,10 @@ def set_debug(val):
15
15
log = logging .getLogger ("uasyncio.core" )
16
16
17
17
18
+ class TimeoutError (Exception ):
19
+ pass
20
+
21
+
18
22
class EventLoop :
19
23
20
24
def __init__ (self , len = 42 ):
@@ -86,6 +90,9 @@ def run_forever(self):
86
90
log .debug ("Coroutine %s send args: %s" , cb , args )
87
91
if args == ():
88
92
ret = next (cb )
93
+ elif isinstance (args [0 ], Exception ):
94
+ print ("Throwing %r into %r" % (args , cb ))
95
+ cb .throw (* args )
89
96
else :
90
97
ret = cb .send (* args )
91
98
if __debug__ and DEBUG :
@@ -215,6 +222,36 @@ def __next__(self):
215
222
sleep_ms = SleepMs ()
216
223
217
224
225
+ class TimeoutObj :
226
+ def __init__ (self , coro ):
227
+ self .coro = coro
228
+ self .active = True
229
+ def cancel (self ):
230
+ self .coro = None
231
+ self .active = False
232
+
233
+
234
+ def wait_for (coro , timeout ):
235
+
236
+ def waiter (coro , timeout_obj ):
237
+ res = yield from coro
238
+ timeout_obj .cancel ()
239
+ return res
240
+
241
+ def timeout_func (timeout_obj ):
242
+ if timeout_obj .active :
243
+ print ("timeout_func: cancelling" , coro )
244
+ timeout_obj .coro .pend_throw (TimeoutError ())
245
+ else :
246
+ print ("timeout_func: timeout was cancelled" )
247
+
248
+ timeout_obj = TimeoutObj (coro )
249
+ _event_loop .call_later (timeout , timeout_func , timeout_obj )
250
+ w = waiter (coro , timeout_obj )
251
+ res = yield from w
252
+ return res
253
+
254
+
218
255
def coroutine (f ):
219
256
return f
220
257
0 commit comments