docs/examples/onboarding/flask/lifespan.py
raw| 1 | from collections.abc import AsyncIterator |
| 2 | from contextlib import asynccontextmanager |
| 3 | |
| 4 | from litestar import Litestar, get |
| 5 | |
| 6 | |
| 7 | @asynccontextmanager |
| 8 | async def lifespan(app: Litestar) -> AsyncIterator[None]: |
| 9 | # Setup: open database pool, warm caches, etc. |
| 10 | yield |
| 11 | # Teardown: close resources. |
| 12 | |
| 13 | |
| 14 | @get("/") |
| 15 | async def index() -> dict[str, str]: |
| 16 | return {"hello": "world"} |
| 17 | |
| 18 | |
| 19 | app = Litestar(route_handlers=[index], lifespan=[lifespan]) |