Python, properly · Chapter 38 of 42
async and await
By the end of this chapter you can write coroutines with async def, run many slow calls at once with asyncio.gather, give them a deadline, cap how many run together with a semaphore, and back off politely when an API says "too many requests". Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world Python
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Bhavna's new screen shows every rider's position and the kitchen's queue. Each answer from the API takes about half a second. Eight riders, one after the other, is four seconds of a frozen screen, and Bhavna refreshes it all day.
Last chapter's code calls the API one request at a time: it sends, then sits still until the answer comes back. Almost all of that time the program is doing nothing at all; it is waiting for the network. The AI labs make this worse: a model call takes seconds, and a RAG pipeline makes dozens of them. async is how Python waits for many things at once, and how the AI libraries you will use (httpx, the model SDKs, LangChain) are built.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
waits.py: every slow call at once
GET http://localhost:8000/slow?ms=500 answers after 500 milliseconds with {"waited_ms": 500}, like a slow service.
Write the coroutine all_waits(ms_list) that calls /slow once for each number in ms_list, all at the same time, with httpx.AsyncClient. It returns the waited_ms of each answer in the order of ms_list: all_waits([300, 100, 200]) returns [300, 100, 200], and all_waits([]) returns [].
The check times four calls of 500 ms: they must take well under 2 seconds.
- Task 2
deadline.py: a deadline
Bhavna's screen cannot wait long for one slow answer. Write the coroutine with_deadline(ms, seconds) that calls /slow?ms=<ms> and gives it at most seconds to answer.
It returns the answer's waited_ms if it came in time, or None if the deadline passed first: with_deadline(200, 1.0) returns 200, and with_deadline(2000, 0.4) returns None after about 0.4 seconds, without raising.
- Task 3
capped.py: at most limit at once
The kitchen system handles only a few requests at a time. Write the coroutine capped_waits(ms_list, limit) that calls /slow?ms=<ms> for every number in ms_list, concurrently, but never with more than limit calls running at the same moment. It returns the waited_ms of each answer, in the order of ms_list.
The check times six calls of 300 ms: with limit 2 they take about 0.9 seconds (three rounds of two), with limit 6 about 0.3 seconds.
- Task 4 · interview question
Interview: polite.py, honour Retry-After
GET /limited?key=... allows 5 calls a second for a key; the sixth gets 429 Too Many Requests with the header Retry-After: 1, the seconds to wait.
Write the coroutine polite_get(client, url, tries=5) that GETs url with the AsyncClient it is given. On a 429 it waits as many seconds as Retry-After says, then asks again, for at most tries calls in all. It returns the first response that is not a 429 (or the last 429 when the tries run out).
The check runs eight polite_get calls at once on a fresh key and counts every answer the API sends: all eight must end with 200, after about 1 second, with only a few 429s along the way.
What you will learn
- Coroutines: async def, await, and running them with asyncio.run.
- The event loop: one thread that switches between tasks whenever one of them waits.
- asyncio.gather: start many calls together, get the results in order; and the trap of blocking the loop.
- Deadlines, semaphores and rate limits: asyncio.timeout, asyncio.Semaphore, and the 429 status with its Retry-After header.
More in Real-world Python
Build "async and await" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.