QUERY · ISSUE
asyncio implement cpython all_tasks
enhancement
It would be very useful to have the ability to get a list of all tasks similar to the cpython implementation.
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)
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).