Incorrect uasyncio behaviour: tasks are not correctly terminated
(First reported on the forum)
The following snippet:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
async def count():
i = 0
while True:
print(i)
i += 1
await asyncio.sleep(1)
async def main():
asyncio.create_task(count())
await asyncio.sleep(5)
asyncio.run(main())
asyncio.run(main())
Produces the following in CPython:
0
1
2
3
4
0
1
2
3
4
But in MicroPython:
0
1
2
3
4
0
5
1
6
2
7
3
8
4
9
It appears that tasks are not correctly terminated (?).
A workaround that produces the correct output is to run the second task in a second event loop:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
async def count():
i = 0
while True:
print(i)
i += 1
await asyncio.sleep(1)
async def main():
asyncio.create_task(count())
await asyncio.sleep(5)
asyncio.run(main())
asyncio.new_event_loop() # Added line
asyncio.run(main())
Note that running either of the snippets repeatedly causes more confusing output because all previous tasks are still running on the queue.
extmod/uasyncio Discrepancy between behaviour and docs
The doc for the class Task states:
Task.cancel()
Cancel the task by injecting a CancelledError into it. The task may or may not ignore this exception.
The following script produces identical results in CPython 3.8 and MicroPython, with no exception occurring in bar(). The script runs correctly apart from this.
Is there in fact a way to detect cancellation within a task, or are the docs wrong?
try:
import uasyncio as asyncio
except ImportError:
import asyncio
async def bar():
try:
while True:
print('Bar running')
await asyncio.sleep(1)
except Exception as e:
print('bar stopped.', e) # Never happens (CPython or mp)
async def main():
t = asyncio.create_task(bar())
await asyncio.sleep(2)
t.cancel()
await asyncio.sleep(1)
asyncio.run(main())
Detecting cancellation or timeouts within a task is actually very useful e.g. for cleanup, if there is a way to achieve it.