0
0
FastAPIframework~5 mins

Startup and shutdown events in FastAPI - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A@app.on_start()
B@app.startup()
C@app.event('start')
D@app.on_event('startup')
When does a shutdown event run in a FastAPI app?
AWhen the app stops
BWhen the app starts
CBefore each request
DAfter each response
Can startup and shutdown event functions be synchronous in FastAPI?
AOnly startup can be sync
BNo, they must be async
CYes, but async is recommended
DOnly shutdown can be sync
What is a common use case for a startup event?
AClosing database connections
BOpening database connections
CHandling HTTP requests
DLogging user activity
What happens if you forget to close resources in a shutdown event?
AIt can cause resource leaks or errors
BThe app will restart
CResources close automatically
DNothing, shutdown events are optional
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.