FastAPI Textbook Ch. 7 — Async, Concurrency, and the Threadpool
Concurrency in FastAPI: async vs sync handlers, the threadpool, AnyIO, blocking traps, structured concurrency, and how to scale.
Concurrency in FastAPI: async vs sync handlers, the threadpool, AnyIO, blocking traps, structured concurrency, and how to scale.
Cheatsheet: pytest setup, AsyncClient, real Postgres via testcontainers, factories, drift, migrations.
Cheatsheet: BackgroundTasks for fire-and-forget, ARQ / Celery / Dramatiq enqueue patterns, idempotency.
Real-time FastAPI: WebSockets, broadcast / pub-sub, SSE for one-way streams, StreamingResponse, NDJSON, and disconnect handling.
Cheatsheet: Dockerfile, K8s Deployment, migration Job, secrets, probes, graceful shutdown.
Cheatsheet: AsyncClient + pytest-anyio, dependency overrides, testcontainers Postgres, factory_boy, OpenAPI tests.
Background tasks in FastAPI: BackgroundTasks for fire-and-forget, ARQ / Celery / Dramatiq for reliable jobs, scheduling, and long-running operations.
Cheatsheet: structlog with trace IDs, OTEL auto-instrumentation, Prometheus, slow-query log.
Cheatsheet: structlog with contextvars, OTEL auto-instrumentation, custom spans, Prometheus middleware, /metrics.
Testing FastAPI: TestClient and AsyncClient, dependency overrides, real DB testing with testcontainers, factory fixtures, and CI integration.