← index #9737Issue #7901
Related · high · value 1.724
QUERY · ISSUE

asyncio implement cpython all_tasks

openby ThinkTransitopened 2022-10-25updated 2022-10-25
enhancement

It would be very useful to have the ability to get a list of all tasks similar to the cpython implementation.

all_tasks

Currently to track all running tasks, tasks must be stored in a global list variable which is not ideal and error prone.

  • Would you be willing to sponsor this work? Yes
CANDIDATE · ISSUE

asyncio: cancelling gather only cancels 1st awaited task (differs from CPython)

closedby jdtsmithopened 2021-10-14updated 2022-03-30
extmod

In CPython, when a task awaiting asyncio.gather is cancelled, gather gets cancelled, and then all tasks gather is awaiting are cancelled. In MicroPython v1.17, it seems only the first gather task gets cancelled:

import uasyncio as asyncio
task = None
async def test1(r):
    while True:
        print(f"Test1 {r}")
        await asyncio.sleep(1)
        
async def test2(r):
    while True:
        print(f"Test2 {r}")
        await asyncio.sleep(1.65)

async def cancel():
    await asyncio.sleep(10)
    task.cancel()

async def main(r):
    await asyncio.gather(test1(r), test2(r), cancel())

async def run():
    global task
    for i in range(10):
        task = asyncio.create_task(main(i))
        try:
            await task
        except BaseException as e:
            print(f"Caught exception {e}")
            pass

asyncio.run(run())

Note that test2 tasks keep accumulating, whereas test1 gets cancelled correctly. Interestingly, these uncancelled tasks stay in the event loop even after run() completes normally, so that future runs include the "zombie" test2's.

Both of these behavior differ from CPython (v3.9.7).

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