@@ -255,8 +255,9 @@ async def bar(x):
255
255
await asyncio.sleep(1 ) # Pause 1s
256
256
257
257
async def main ():
258
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
258
259
for x in range (3 ):
259
- asyncio.create_task(bar(x))
260
+ tasks[x] = asyncio.create_task(bar(x))
260
261
await asyncio.sleep(10 )
261
262
262
263
asyncio.run(main())
@@ -342,21 +343,40 @@ async def bar(x):
342
343
await asyncio.sleep(1 ) # Pause 1s
343
344
344
345
async def main ():
346
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
345
347
for x in range (3 ):
346
- asyncio.create_task(bar(x))
348
+ tasks[x] = asyncio.create_task(bar(x))
347
349
print (' Tasks are running' )
348
350
await asyncio.sleep(10 )
349
351
350
352
asyncio.run(main())
351
353
```
352
- ##### Note
354
+ ### Note on CPython compatibility
353
355
354
356
The CPython [ docs] ( https://docs.python.org/3/library/asyncio-task.html#creating-tasks )
355
357
have a warning that a reference to the task instance should be saved for the
356
358
task's duration. This was raised in
357
359
[ this issue] ( https://github.com/micropython/micropython/issues/12299 ) .
358
360
MicroPython ` asyncio ` does not suffer from this bug, but writers of code which
359
- must work in CPython and MicroPython should take note.
361
+ must work in CPython and MicroPython should take note. Code samples in this doc
362
+ are CPython-compatible, but following version is valid in MicroPython:
363
+ ``` python
364
+ import asyncio
365
+ async def bar (x ):
366
+ count = 0
367
+ while True :
368
+ count += 1
369
+ print (' Instance: {} count: {} ' .format(x, count))
370
+ await asyncio.sleep(1 ) # Pause 1s
371
+
372
+ async def main ():
373
+ for x in range (3 ):
374
+ asyncio.create_task(bar(x)) # No reference stored
375
+ print (' Tasks are running' )
376
+ await asyncio.sleep(10 )
377
+
378
+ asyncio.run(main())
379
+ ```
360
380
361
381
###### [ Contents] ( ./TUTORIAL.md#contents )
362
382
@@ -491,7 +511,7 @@ def set_global_exception():
491
511
async def main ():
492
512
set_global_exception() # Debug aid
493
513
my_class = MyClass() # Constructor might create tasks
494
- asyncio.create_task(my_class.foo()) # Or you might do this
514
+ task = asyncio.create_task(my_class.foo()) # Or you might do this
495
515
await my_class.run_forever() # Non-terminating method
496
516
try :
497
517
asyncio.run(main())
@@ -613,8 +633,9 @@ async def task(i, lock):
613
633
614
634
async def main ():
615
635
lock = Lock() # The Lock instance
636
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
616
637
for n in range (1 , 4 ):
617
- asyncio.create_task(task(n, lock))
638
+ tasks[n - 1 ] = asyncio.create_task(task(n, lock))
618
639
await asyncio.sleep(10 )
619
640
620
641
asyncio.run(main()) # Run for 10s
@@ -642,8 +663,9 @@ async def task(i, lock):
642
663
643
664
async def main ():
644
665
lock = Lock() # The Lock instance
666
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
645
667
for n in range (1 , 4 ):
646
- asyncio.create_task(task(n, lock))
668
+ tasks[n - 1 ] = asyncio.create_task(task(n, lock))
647
669
await asyncio.sleep(10 )
648
670
649
671
asyncio.run(main()) # Run for 10s
@@ -671,7 +693,7 @@ async def waiter(event):
671
693
672
694
async def main ():
673
695
event = Event()
674
- asyncio.create_task(waiter(event))
696
+ task = asyncio.create_task(waiter(event))
675
697
await asyncio.sleep(2 )
676
698
print (' Setting event' )
677
699
event.set()
@@ -821,7 +843,7 @@ async def main():
821
843
tasks = [asyncio.create_task(bar(70 ))]
822
844
tasks.append(barking(21 ))
823
845
tasks.append(asyncio.wait_for(foo(10 ), 7 ))
824
- asyncio.create_task(do_cancel(tasks[0 ]))
846
+ can = asyncio.create_task(do_cancel(tasks[0 ]))
825
847
res = None
826
848
try :
827
849
res = await asyncio.gather(* tasks, return_exceptions = True )
@@ -939,8 +961,9 @@ async def foo(n, sema):
939
961
940
962
async def main ():
941
963
sema = Semaphore()
964
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
942
965
for num in range (3 ):
943
- asyncio.create_task(foo(num, sema))
966
+ tasks[num] = asyncio.create_task(foo(num, sema))
944
967
await asyncio.sleep(2 )
945
968
946
969
asyncio.run(main())
@@ -1022,8 +1045,8 @@ async def consume(queue):
1022
1045
1023
1046
async def queue_go (delay ):
1024
1047
queue = Queue()
1025
- asyncio.create_task(consume(queue))
1026
- asyncio.create_task(produce(queue))
1048
+ t1 = asyncio.create_task(consume(queue))
1049
+ t2 = asyncio.create_task(produce(queue))
1027
1050
await asyncio.sleep(delay)
1028
1051
print (" Done" )
1029
1052
@@ -1188,8 +1211,9 @@ async def main():
1188
1211
sw1 = asyncio.StreamWriter(UART(1 , 9600 ), {})
1189
1212
sw2 = asyncio.StreamWriter(UART(2 , 1200 ), {})
1190
1213
barrier = Barrier(3 )
1214
+ tasks = [None ] * 2 # For CPython compaibility must store a reference see Note
1191
1215
for n, sw in enumerate ((sw1, sw2)):
1192
- asyncio.create_task(sender(barrier, sw, n + 1 ))
1216
+ tasks[n] = asyncio.create_task(sender(barrier, sw, n + 1 ))
1193
1217
await provider(barrier)
1194
1218
1195
1219
asyncio.run(main())
@@ -1321,8 +1345,9 @@ async def foo(n, d):
1321
1345
1322
1346
async def my_app ():
1323
1347
d = Delay_ms()
1348
+ tasks = [None ] * 4 # For CPython compaibility must store a reference see Note
1324
1349
for n in range (4 ):
1325
- asyncio.create_task(foo(n, d))
1350
+ tasks[n] = asyncio.create_task(foo(n, d))
1326
1351
d.trigger(3000 )
1327
1352
print (' Waiting on d' )
1328
1353
await d.wait()
@@ -1632,7 +1657,7 @@ async def foo():
1632
1657
print (' Does not print' ) # Because bar() raised an exception
1633
1658
1634
1659
async def main ():
1635
- asyncio.create_task(foo())
1660
+ task = asyncio.create_task(foo())
1636
1661
for _ in range (5 ):
1637
1662
print (' Working' ) # Carries on after the exception
1638
1663
await asyncio.sleep(0.5 )
@@ -1675,7 +1700,7 @@ async def bar():
1675
1700
async def main ():
1676
1701
loop = asyncio.get_event_loop()
1677
1702
loop.set_exception_handler(_handle_exception)
1678
- asyncio.create_task(bar())
1703
+ task = asyncio.create_task(bar())
1679
1704
for _ in range (5 ):
1680
1705
print (' Working' )
1681
1706
await asyncio.sleep(0.5 )
@@ -2270,7 +2295,7 @@ class PinCall(io.IOBase):
2270
2295
self .cbf_args = cbf_args
2271
2296
self .pinval = pin.value()
2272
2297
self .sreader = asyncio.StreamReader(self )
2273
- asyncio.create_task(self .run())
2298
+ self .task = asyncio.create_task(self .run())
2274
2299
2275
2300
async def run (self ):
2276
2301
while True :
@@ -2680,7 +2705,7 @@ class LED_async():
2680
2705
def __init__ (self , led_no ):
2681
2706
self .led = pyb.LED(led_no)
2682
2707
self .rate = 0
2683
- asyncio.create_task(self .run())
2708
+ self .task = asyncio.create_task(self .run())
2684
2709
2685
2710
async def run (self ):
2686
2711
while True :
0 commit comments