Recall & Review
beginner
What are startup events in FastAPI?
Startup events are functions that run once when the FastAPI app starts. They are used to set up resources like database connections or caches before the app handles requests.
Click to reveal answer
beginner
How do you register a startup event in FastAPI?
Use the @app.on_event('startup') decorator above an async function. This function will run automatically when the app starts.
Click to reveal answer
beginner
What are shutdown events in FastAPI?
Shutdown events are functions that run once when the FastAPI app is stopping. They help clean up resources like closing database connections or saving state.
Click to reveal answer
beginner
How do you register a shutdown event in FastAPI?
Use the @app.on_event('shutdown') decorator above an async function. This function will run automatically when the app is stopping.
Click to reveal answer
intermediate
Why are startup and shutdown events important in FastAPI?
They help manage resources safely and efficiently by preparing needed services before requests and cleaning up after the app stops, preventing errors and leaks.
Click to reveal answer
Which decorator is used to define a startup event in FastAPI?
✗ Incorrect
The correct decorator is @app.on_event('startup') to register a startup event.
When does a shutdown event run in a FastAPI app?
✗ Incorrect
Shutdown events run once when the app is stopping to clean up resources.
Can startup and shutdown event functions be synchronous in FastAPI?
✗ Incorrect
They can be synchronous, but async functions are recommended for non-blocking operations.
What is a common use case for a startup event?
✗ Incorrect
Startup events often open database connections or initialize resources.
What happens if you forget to close resources in a shutdown event?
✗ Incorrect
Not closing resources can cause leaks or errors, so shutdown events help prevent that.
Explain how to use startup and shutdown events in FastAPI and why they matter.
Think about preparing and cleaning up resources around the app lifecycle.
You got /5 concepts.
Describe a real-life example where startup and shutdown events improve a FastAPI app.
Imagine your app needs a database to work smoothly.
You got /4 concepts.