Python asyncio Recipes/Inspect running tasks

List currently active tasks in the running loop.

Section: Debugging and Introspection

Inspect running tasks

python
python
import asyncio

async def sleeper():
    await asyncio.sleep(1)

async def main():
    task = asyncio.create_task(sleeper(), name="demo-task")
    for t in asyncio.all_tasks():
        print(t.get_name())
    await task

asyncio.run(main())
Explanation

Use task introspection when debugging leaked or stuck coroutines.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Debugging and Introspection
Enable asyncio debug mode
Turn on extra diagnostics while developing.
OpenIn sheetpythonsame section
Bound a worker pool with Semaphore
Throttle async work to a safe level.
Retry an async operation
Retry with simple exponential backoff.
Process input in batches
Break large workloads into fixed-size async chunks.
Cancel cleanly on shutdown
Maintain and cancel outstanding tasks at shutdown time.