Start here. This is the direct spoken answer to practice first.
Overview
Lifespan is the process-level context around serving requests, not a place for arbitrary work before the first request.
A FastAPI lifespan context owns resources that should be created once for an application process and released when that process stops. Typical examples are shared HTTP clients, connection pools, loaded models, or managed background loops. Code before the lifespan yield performs startup, and code after it performs shutdown. Requests should not be accepted as ready until required startup succeeds, and cleanup should close partially acquired resources if startup or shutdown fails.