Python 3.7+Async programmingfunction
asyncio.all_tasks()
introspection
Returns the set of not-yet-finished tasks of the running event loop, including the task that made the call.
Syntax
asyncio.all_tasks(loop=None)
The set includes the calling task itself (main under asyncio.run), so "wait for all the others" is written as all_tasks() - {current_task()}; otherwise gather would wait for itself forever. Finished tasks are not reported. Without a running loop the function raises RuntimeError; the old Task.all_tasks() method was removed in 3.9.
Examples
import asyncio
async def job():
await asyncio.sleep(0.01)
async def main():
tasks = [asyncio.create_task(job()) for _ in range(2)]
print(len(asyncio.all_tasks())) # → 3 — две job и сама main
others = asyncio.all_tasks() - {asyncio.current_task()}
await asyncio.gather(*others)
print(len(asyncio.all_tasks())) # → 1
asyncio.run(main())
import asyncio
try:
asyncio.all_tasks()
except RuntimeError as e:
print(e) # → no running event loop
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy