Glossary coding Term Page

Lifespan Hook

Async context manager that wires initialization and cleanup around the app lifecycle.

lifespan-hook #fastapi#startup#operations
Korean version

Aliases

lifespan-hooklifespan-event

Related Concepts

Core Idea

The lifespan hook is an async context manager registered on the FastAPI application. It runs once at startup and once at shutdown, so it is ideal for opening database pools, warm caches, seeding feature flags, and releasing those resources gracefully when the process stops.

Why It Matters Here

Mathbong's mini service chapters rely on lifespan hooks to set up SQLModel engines, logging sinks, and shared middleware state before routers start serving traffic. Treating lifecycle work separately from request handlers keeps tutorials focused on per-request logic.

Posts Mentioning This Concept