Share State via App Context
Pass shared objects into every handler.
State That Every Handler Needs
Your tools often need the same objects, like one database pool. You want to create them once and share them everywhere.
The Yielded Value Is the State
Whatever the lifespan yields becomes your shared application state, available to every tool, resource, and prompt.
async def lifespan(server):
yield {"db": db, "cache": cache}All lessons in this course
- The Lifespan Startup Hook
- Share State via App Context
- Access the Context Object
- Clean Shutdown & Resource Release