← index #7471Issue #5881
Related · high · value 2.354
QUERY · ISSUE

Incorrect uasyncio behaviour: tasks are not correctly terminated

openby mattytrentiniopened 2021-06-29updated 2021-07-05
extmod

(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.

CANDIDATE · ISSUE

extmod/uasyncio Discrepancy between behaviour and docs

closedby peterhinchopened 2020-04-08updated 2020-04-08

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied