@@ -400,37 +400,46 @@ are subject to cancellation or timeout.
400
400
import uasyncio as asyncio
401
401
import asyn
402
402
403
+ async def barking (n ):
404
+ print (' Start normal coro barking()' )
405
+ for _ in range (6 ):
406
+ await asyncio.sleep(1 )
407
+ print (' Done barking.' )
408
+ return 2 * n
409
+
403
410
async def foo (n ):
404
- while True :
405
- try :
411
+ print (' Start timeout coro foo()' )
412
+ try :
413
+ while True :
406
414
await asyncio.sleep(1 )
407
415
n += 1
408
- except asyncio.TimeoutError:
409
- print (' foo timeout' )
410
- return n
416
+ except asyncio.TimeoutError:
417
+ print (' foo timeout. ' )
418
+ return n
411
419
412
420
@asyn.cancellable
413
421
async def bar (n ):
414
- while True :
415
- try :
422
+ print (' Start cancellable bar()' )
423
+ try :
424
+ while True :
416
425
await asyncio.sleep(1 )
417
426
n += 1
418
- except asyn.StopTask:
419
- print (' bar stopped' )
420
- return n
427
+ except asyn.StopTask:
428
+ print (' bar stopped. ' )
429
+ return n
421
430
422
431
async def do_cancel ():
423
- await asyncio.sleep(5 )
432
+ await asyncio.sleep(5.5 )
424
433
await asyn.Cancellable.cancel_all()
425
434
426
-
427
435
async def main (loop ):
428
- bar_task = asyn.Cancellable(bar, 70 )
429
- gatherables = [asyn.Gatherable(bar_task),]
430
- gatherables.append(asyn.Gatherable(foo, 10 , timeout = 7 ))
436
+ bar_task = asyn.Cancellable(bar, 70 ) # Note args here
437
+ gatherables = [asyn.Gatherable(barking, 21 ),
438
+ asyn.Gatherable(foo, 10 , timeout = 7.5 ),
439
+ asyn.Gatherable(bar_task)]
431
440
loop.create_task(do_cancel())
432
441
res = await asyn.Gather(gatherables)
433
- print (' Result: ' , res)
442
+ print (' Result: ' , res) # Expect [42, 17, 75]
434
443
435
444
loop = asyncio.get_event_loop()
436
445
loop.run_until_complete(main(loop))
0 commit comments