Guide31K views1,459 votes0 replies1,852 following
FastAPI Lifespan and app.state: Managing Process-Wide Resources
Use FastAPI's lifespan context manager to create and dispose shared clients or pools once at startup and shutdown, expose them via app.state, and keep request-scoped cleanup in dependencies with yield.