Python course / Applied
Async syntax and event loops
Write coroutines that wait without blocking, and run many of them at once. Async is about waiting, not speed, and one blocking call freezes every task you started.
- async and await
- gather
- tasks
- timeouts
- cancellation
- TaskGroup
- queues
- semaphores
Runs in your browser. Start straight away; create an account when you want your progress kept.
import asyncio
import time
async def fetch(name, seconds):
await asyncio.sleep(seconds)
return f"{name} after {seconds}s"
async def main():
started = time.perf_counter()
results = await asyncio.gather(
fetch("tea", 1), fetch("jam", 1), fetch("oats", 1)
)
for line in results:
print(line)
print(f"elapsed: {time.perf_counter() - started:.1f}s")
asyncio.run(main())
tea after 1s jam after 1s oats after 1s elapsed: 1.0s
What you will learn
- Write a coroutine function with async def, wait inside it with await asyncio.sleep, and run it with asyncio.run.
- See that awaits one after another take the sum of their waits, and time an awaitable with a helper.
- Run several coroutines concurrently with asyncio.gather, and know its results come back in argument order.
- Put a time limit on an await with asyncio.timeout, and know the work that ran out of time is cancelled.
- Run tasks in an asyncio.TaskGroup, which waits for all of them and cancels the rest when one fails, and handle its errors with except*.
- Cap how many coroutines do something at the same time with asyncio.Semaphore.
The lessons in this module
- Waiting, not working
- A coroutine is an object
- Awaiting in sequence
- gather: waiting on several at once
- Tasks: starting work now
- An event loop by hand
- Blocking the loop
- Timeouts
- Cancellation
- Errors in gather
- TaskGroup: all or nothing
- Queues: sharing out the work
- Limiting how many at once
- async with and async for
- Capstone: a polite crawler
- Module review: the order board
Ready when you are
Previous: functools and itertools. Next: Testing. All modules are on the course page.