QUERY · ISSUE
uasyncio: Behaviour differs from CPython.
bug
Consider this script:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
evt = asyncio.Event()
async def foo():
try:
await evt.wait()
finally:
print('Finally')
async def main():
t = asyncio.create_task(foo())
await asyncio.sleep(1)
t. cancel()
# Can be fixed with .sleep(0) here
print('canned')
await asyncio.sleep(1)
asyncio.run(main())
On MP the outcome is:
MicroPython v1.19.1-617-g43dd3ea74 on 2022-11-08; linux [GCC 9.4.0] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats
canned
Finally
>>>
On CPython:
$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats
Finally
canned
>>>
MP behaviour also confirmed on Pyboard.
CANDIDATE · ISSUE
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.