How does lifecycle differ for global vs path operation dependencies?

WHAT IT TESTS: Per-request scoping in FastAPI. ANSWER OUTLINE: Global deps run on every request to any route; path-local deps run only for that route; expensive setup belongs in a lifespan event or cached singleton, not a dependency.
WHAT IT TESTS: Whether you know FastAPI dependencies resolve per-request and that scoping matters for expensive work. ANSWER OUTLINE: First, both run per-request, but a global dep runs for every request to any endpoint while a path-local dep runs only for that route. Second, expensive setup inside a dependency blocks the request, so heavy work belongs in a lifespan event or cached module singleton, with the dependency acting as a lightweight accessor. RED FLAG: Claiming global deps run once at startup or are cached across requests.
Read the original → fastapi.tiangolo.com
- #fastapi
- #dependency-injection
- #python
- #performance
- #lifespan-events
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.