Exercise py-12 — the F1 API, made fully async

In a synchronous framework, one blocking database call just makes that request slow. In an async one it stalls every other request sharing the event loop — the failure mode changes shape entirely. You extend py-10's service with a pooled connection layer (asyncpg-shaped; a FakePool stands in for Postgres in the tests) that cloud-09's RDS instance will sit behind for real, a /readyz that reflects the pool's health instead of a stub model's, and a shutdown that drains in-flight requests before it closes the pool out from under them. It runs on your Mac rather than in the browser for the same reason py-10 does: Pyodide has no FastAPI, no real ASGI server, and no connection pool to exhaust.

~90 minruns locally · uv + pytest 6 checkspy-12-async-service

What you're building

The tests are ordinary pytest and ship in the public folder with the starter — read them first; the names below are the check list. Solutions are not published.

Get the repo (once)

Needs git. uv installs the right Python itself, so nothing else is required.

# once, anywhere on your machine
git clone https://github.com/theDocWho/ai-ml-roadmap.git
cd ai-ml-roadmap

No git? Download the ZIP, unzip it, and cd into the unzipped folder instead.

Run it

From the repo root:

# one-time: uv (https://docs.astral.sh/uv/) manages the venv and pins Python ≥ 3.12
cd exercises/py-12-async-service && uv sync && uv run pytest -q

# once the tests pass: talk to it for real (needs a reachable Postgres — cloud-09's RDS, or any local one)
DATABASE_URL=postgresql://... uv run uvicorn f1api.main:app --port 8000 &
curl -s -X POST localhost:8000/ask -H 'content-type: application/json' -d '{"prompt": "hi"}'
curl -si localhost:8000/readyz

# load test — watch p95 climb once concurrent users pass F1API_POOL_MAX
uv run locust -f locustfile.py --host http://127.0.0.1:8000

# the same bar the reference solution clears
uv run ruff check . && uv run mypy src

Done when uv run pytest -q prints 6 passed. The untouched starter fails all six.

The checks

Files

This is self-attestation — the site cannot see your terminal, so the box and the button are you telling The Path the suite went green on your machine.

If you get stuck