Learn asyncio

Python's built-in framework for concurrent I/O with async and await

16 lessons · 3 hours · 4 levels · covers asyncio Python 3.13

asyncio is the standard library's event loop and toolkit for writing concurrent code with async/await. It lets one thread juggle thousands of network connections, API calls or subprocesses, and it underpins async web frameworks such as FastAPI, aiohttp and many database drivers. Learn it to write fast I/O-bound programs and to understand what async libraries do under the hood.

Before you start

asyncio builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 46 min in total
2. PracticalUse it productively at work, with the usual idioms10 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs13 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know16 lessons · 3 hours in total

The asyncio tree

asyncioCoroutines and asyncio.runCoroutines andasyncio.runConcurrency vs blockingConcurrency vsblockingTasks: running coroutines concurrentlyTasks: runningcoroutines concurr…Waiting for many: gatherWaiting for many:gatherTimeoutsTimeoutsTaskGroup and structured concurrencyTaskGroup andstructured concurr…Cancellation and cleanupCancellation andcleanupProducer-consumer with asyncio.QueueProducer-consumerwith asyncio.QueueLocks, semaphores and eventsLocks, semaphoresand eventsRunning blocking code: to_threadRunning blockingcode: to_threadDefining functionsDefining functionsExceptions in depthExceptions in depthContext managers and withContext managersand withThreads, processes and concurrent.futuresThreads, processesand concurrent.fut…
Essentials and Practical shown (10 of 16 lessons); the full list is below. Click a lesson to read it.

All 16 lessons, in order

Coroutines and the event loop

  1. Coroutines and asyncio.run · Essentials · 10 min

    Define coroutines with async def, call them with await, and start the event loop with asyncio.run().

  2. Concurrency vs blocking · Essentials · 9 min

    See why asyncio.sleep lets other coroutines run while time.sleep freezes the whole event loop.

Tasks and structured concurrency

  1. Tasks: running coroutines concurrently · Essentials · 10 min

    Wrap coroutines in Tasks with asyncio.create_task so they run in the background while you do other work.

  2. Waiting for many: gather · Essentials · 9 min

    Run several awaitables concurrently and collect their results in order with asyncio.gather.

  3. Timeouts · Essentials · 8 min

    Bound how long an operation may take with asyncio.timeout() or asyncio.wait_for().

  4. TaskGroup and structured concurrency · Practical · 12 min

    Use asyncio.TaskGroup to start related tasks that are always awaited together and cancelled together on failure.

  5. Cancellation and cleanup · Practical · 12 min

    Cancel tasks with task.cancel(), clean up with try/finally, and know when to re-raise CancelledError.

  6. Processing results as they finish · Advanced · 9 min

    Handle results in completion order with asyncio.as_completed, or wait for the first one with asyncio.wait.

Coordination and queues

  1. Producer-consumer with asyncio.Queue · Practical · 11 min

    Pass work between coroutines with asyncio.Queue, using a fixed pool of worker tasks.

  2. Locks, semaphores and events · Practical · 10 min

    Coordinate coroutines with asyncio.Lock, Semaphore and Event.

I/O and integration

  1. Running blocking code: to_thread · Practical · 10 min

    Offload blocking or legacy sync calls to threads with asyncio.to_thread or to processes with run_in_executor.

  2. async with, async for and async generators · Advanced · 12 min

    Write async context managers and async iterators, including async generators and asynccontextmanager.

  3. Network I/O with streams · Advanced · 14 min

    Write TCP clients and servers with asyncio.open_connection and asyncio.start_server.

Internals and performance

  1. Inside the event loop · Expert · 15 min

    Understand how the loop schedules callbacks, how Futures and Tasks drive coroutines, and how await really suspends.

  2. Debug mode and common bugs · Expert · 12 min

    Use asyncio debug mode to catch never-awaited coroutines, slow callbacks, unretrieved errors and thread misuse.

  3. Eager tasks and performance · Expert · 12 min

    Speed up asyncio with eager task execution, fewer task allocations and alternative loops like uvloop.