FastAPI Cheatsheet 06 — Async, Concurrency, and the Threadpool
Cheatsheet: async def vs def, AnyIO/TaskGroup, to_thread, blocking traps, semaphores, timeouts, cancellation.
Cheatsheet: async def vs def, AnyIO/TaskGroup, to_thread, blocking traps, semaphores, timeouts, cancellation.
Auth and authz in FastAPI: passwords, JWT, OAuth2 / OIDC, sessions, passkeys, RBAC, scopes, and security best practices.
Cheatsheet: WebSocket handlers, auth patterns, broadcast (single + multi-process), SSE, NDJSON, heartbeats.
Concurrency in FastAPI: async vs sync handlers, the threadpool, AnyIO, blocking traps, structured concurrency, and how to scale.
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: 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 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.