knaxuslearn · build · ship

Roadmaps / Python / Phase 3

Phase 3 of 4 · Study weeks 9-12

Async, performance and durability

This phase makes the third checkpoint possible: a service that holds its latency target and loses no acknowledged event.

Topics every week

  1. Week 98 h
    • The event loop and asyncio
    • Tasks, cancellation and timeouts
    • Blocking calls and to_thread
    • Bounded queues and backpressure

    Hand inAn async service with a bounded queue and a test that blocking stalls it

  2. Week 108 h
    • Web layer with FastAPI
    • Dependency injection
    • Sessions and transactions with SQLAlchemy
    • Batched writes

    Hand inBatched inserts and a measured gain over one insert per event

  3. Week 118 h
    • Durability: acknowledge after commit
    • Idempotent writes
    • Crash tests with kill -9
    • Graceful shutdown

    Hand inThe 20-kill table

  4. Week 128 h
    • Load testing
    • Latency percentiles and spread
    • Tracing an async service
    • Moving hot code out of Python (NumPy, Polars, Rust through PyO3)

    Hand inCheckpoint 3, with predicted and measured numbers side by side

Practice projects

  • Practice 1, the blocked loop (Week 9): call a blocking function inside a coroutine. Done when: a trace shows the stall, and to_thread removes it.
  • Practice 2, the batch size (Week 10): write 1, 10, 100 and 1,000 events per insert. Done when: a table shows throughput and p99 for each and you pick the size with a reason.
  • Practice 3, the crash (Week 11): kill the process in the middle of a load run, 20 times. Done when: every acknowledged event is present after restart and the table shows the result.
  • Optional, the vectorised transform: rewrite the hot transform with NumPy or Polars. Done when: a table compares pure Python with the vectorised version and says when the gain stops.

GitHub repos to study

Existence and last commit were checked on 22 Nov 2026 by shallow clone. Every repository below had a commit in the last 12 months.

RepoWhat to readWeek
fastapi/fastapiDependency injection and the OpenAPI generation10
sqlalchemy/sqlalchemySessions, the unit of work and the Core layer10
MagicStack/uvloopWhat a faster event loop changes and what it does not9
pola-rs/polarsThe lazy engine and expression API12
PyO3/pyo3How the interpreter lock is handled in a Rust extension12

Milestone checkpoint

Build Checkpoint 3. Write the load script, your latency prediction and the 20-kill script first. Add the async service, the queue, the pool and the batched writes. Seed a blocking call in the loop and find it from a trace. Hand in the latency report with spread, the kill table, ADR-5 (the acknowledgement rule) and ADR-6 (the batch size and queue bound).

Check yourself

Answer each question in your head first, then open it to check.

  1. What happens when a coroutine calls a blocking function?
    The event loop is stuck until it returns, so every other task waits. Use await on an async call or to_thread for blocking work.
  2. Why use a bounded queue?
    A full queue pushes back on the producer. An unbounded queue moves the overload into memory until the process dies.
  3. When is it safe to acknowledge an event?
    After the commit that makes it durable. An acknowledgement before the commit can be lost on a crash.
  4. Why do batched inserts raise throughput?
    One round trip and one commit cover many rows, so the fixed cost is shared.
  5. When is moving code out of Python the right call?
    When a profile shows one hot loop on large data that a vectorised library or an extension does much faster, and the cost of maintaining it is lower than the cost of the slowness.