David Beazley · Build Your Own Async
Optional, 15 min. Follow the first scheduling example. Point to the line where another job can run while the first job waits.
Start with interactive chat. Write down what the user sees before the first token and what they see while later tokens arrive.
Python 3.11+, a terminal, paper and a new project directory.
Inference Engineering · §1.2.2 p. 29; §1.4 pp. 35–38; §2.2 pp. 46–49
Use the printed page numbers. In the supplied PDF, add two to reach the viewer page. Write a prediction for the question below before opening its answer.
Python · asyncio queues · Baseten · Inference Engineering
Optional, 15 min. Follow the first scheduling example. Point to the line where another job can run while the first job waits.
In 35 minutes implement three async workers consuming ten jobs from a bounded queue. Inject one failure and cancel one caller. Write the first version unaided.
On paper, connect client, gateway, queue, worker and client. Circle the first place a request can wait.
Download the Python starter below. In its folder run python3 pool.py. It should print “finished 0”. Set the job count to ten, then start three consumers.
Download pool.py. Use Python 3.11 or later. Run the starter once, then close it and write your three-worker version in a new file.
Make job 4 raise ValueError. Record the error, finish the other nine and cancel an idle consumer. Put task_done() in a finally block after a successful get().
If join() hangs, count successful puts and calls to task_done(). They must match.
Save request.md with a five-hop trace and two practice targets: first content within 1 s, later tokens averaging at most 80 ms apart. Save your pool implementation.
A request enters at 0 ms, starts prefill at 100 ms and emits at 220 ms. How much of TTFT can you attribute to prefill?
The client measures 220 ms. You need a prefill-end timestamp to isolate computation from sampling, transport and buffering.
Save a trace and three tests. Explain why a missing task_done can hang shutdown.