Lifespan — Startup & Shutdown Done Properly — Concept Check
Questions on Lifespan — Startup & Shutdown Done Properly. The lifespan context manager runs setup before the first request (DB pools, Redis, ML models) and teardown on exit — shared via app.state, probed by health endpoints, and replacing the deprecated on_event hooks.
Questions in this quiz
8 questions on Lifespan — Startup & Shutdown Done Properly — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A FastAPI application is experiencing slow response times after deployment. The health readiness probe shows 'model: missing'. What could be the issue?
- AThe app is using too many worker processes.
- BThe HTTP client session is not reused.
- CThe ML model wasn't loaded during the lifespan setup.
- DThe liveness probe is incorrectly configured.
2.In a FastAPI application using the lifespan pattern, why might you see multiple instances of a resource like a connection pool?
- AEach worker process runs its own lifespan setup.
- BResources are shared across requests but not processes.
- CThe app is incorrectly configured to duplicate resources.
- DThe lifespan pattern automatically duplicates resources.
3.A developer notices that an ML model is being loaded multiple times in a FastAPI application. What is a likely cause?
- AThe lifespan function is being run per worker process.
- BThe model is too large for the application.
- CThe liveness probe is failing.
- DThe model is not compatible with FastAPI.
4.Why might a FastAPI application using the lifespan pattern fail to route traffic even though the process is running?
- AThe application is using deprecated startup hooks.
- BThe readiness probe is failing due to resource issues.
- CThe lifespan function is not yielding correctly.
- DThe liveness probe is not configured properly.
5.A FastAPI service using the lifespan pattern is deployed with 4 Uvicorn workers. How many times will the lifespan setup run?
- AIt depends on the configuration.
- BOnce, shared across all workers.
- CTwice, once for startup and once for shutdown.
- DFour times, once per worker.
6.What is a potential downside of running one-time jobs like database migrations in the lifespan function?
- AThey interfere with the liveness probe.
- BThey may run multiple times, causing conflicts.
- CThey are not executed at all.
- DThey consume too much memory.
7.Why is the lifespan pattern preferred over loading resources at import time in FastAPI?
- AIt simplifies the application code structure.
- BIt avoids breaking tooling and allows for clean setup and teardown.
- CIt ensures resources are loaded for each request.
- DIt reduces the memory footprint of the application.
8.What is the role of a readiness probe in a FastAPI application using the lifespan pattern?
- ATo verify that the application's resources are ready to serve requests.
- BTo check if the process is alive and running.
- CTo manage the number of worker processes.
- DTo initialize resources before the first request.