Async Python, by Running the Loop
The event loop, coroutines and tasks, and the patterns real async code is built from - each one runnable in your browser.
About this track
Async Python confuses people because it looks like threading and is not: it is one thread running an event loop that lets coroutines step aside at every await, so a thousand waits can overlap without a thread each. This track builds that model from the loop up.
Every page runs. The examples end in await main() because the editor already has a loop going, and each one prints the thing being explained - the interleaving of two coroutines, the difference between sequential awaits and concurrent tasks, a blocking call freezing the loop, a bounded queue applying backpressure.
The examples are short on purpose and print the thing being explained - the order two coroutines interleave in, or how a run of sequential awaits compares with tasks that overlap - so what the loop is doing is visible in the output rather than described around it.
What you will be able to do
- Describe what the event loop does on each tick, and why one thread can keep thousands of coroutines moving.
- Explain why calling an async function returns a coroutine rather than a result, and what await actually waits for.
- Turn a run of sequential awaits into tasks that overlap, with gather or a TaskGroup, and decide what happens when one fails.
- Spot a blocking call inside a coroutine, and move it off the loop instead of letting it stall everything else.
- Connect a fast producer to a slow consumer through a bounded queue, so memory stays flat however far ahead the producer gets.
How the track is ordered
The loop comes first, because every later page is a consequence of it: one thread, a queue of ready work, and coroutines that give way at each await. Coroutines and tasks follow, which is where most first async programs go wrong by awaiting one thing after another. Running work concurrently builds on tasks. The blocking call is deliberately fourth - it only makes sense once you have seen what the loop was doing while it froze - and queues close the track because backpressure is a pattern made out of everything before it. It assumes ordinary Python functions and exceptions, nothing more.
Where this leads
Async is one of three ways Python does more than one thing at once. The Concurrency track covers the other two, threads and processes, and ends on how to choose between all three. FastAPI is the most common place this knowledge gets used: its async def endpoints run on an event loop like the one described here.
All 5 modules, in teaching order
- 01The Event Loop, Stepped ThroughStep through Python's event loop tick by tick. See how one thread, one ready queue and cooperative yields run thousands of coroutines.
- 02Coroutines, Tasks and awaitLearn why calling an async function returns a coroutine, not a result, and how create_task turns sequential awaits into real overlap.
- 03Running Work Concurrently: gather and TaskGroupFan out many awaits at once with gather and TaskGroup, collect every result in order, and control what happens when one task fails.
- 04The Blocking Call That Freezes the LoopSee how one synchronous call inside a coroutine freezes every other task, then learn the two ways to move blocking work off the loop.
- 05Queues, Producers and BackpressureConnect a fast producer to a slow consumer with an asyncio queue and watch one maxsize parameter prevent memory from growing without bound.