0
0
FastAPIframework~10 mins

Startup and shutdown events in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Startup and shutdown events
App starts
Run startup event
App ready to handle requests
App receives shutdown signal
Run shutdown event
App stops
When the FastAPI app starts, it runs startup events first. After that, it handles requests. When stopping, it runs shutdown events before fully stopping.
Execution Sample
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.on_event("startup")
async def startup_event():
    print("Starting up...")

@app.on_event("shutdown")
async def shutdown_event():
    print("Shutting down...")
This code prints messages when the app starts and stops using startup and shutdown events.
Execution Table
StepTriggerActionOutputState
1App startsRun startup_eventPrints 'Starting up...'App ready
2App runningHandle requestsRespond to clientsApp ready
3Shutdown signalRun shutdown_eventPrints 'Shutting down...'App stopping
4App stoppedExit processNo outputApp stopped
💡 App stops after running shutdown event on shutdown signal
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
App stateNot startedReadyReadyStoppingStopped
Key Moments - 3 Insights
Why does the startup event run before the app handles requests?
The startup event prepares the app before it can serve requests, as shown in execution_table step 1 where the app prints 'Starting up...' before becoming ready.
What happens if the shutdown event is not defined?
If no shutdown event is defined, the app stops immediately without running any cleanup code, unlike step 3 where shutdown_event prints a message.
Can startup and shutdown events be asynchronous?
Yes, they can be async functions as in the example code, allowing for async operations during startup and shutdown.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the app state after step 1?
AReady
BStopping
CNot started
DStopped
💡 Hint
Check the 'State' column in row for step 1 in execution_table
At which step does the app print 'Shutting down...'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in execution_table for the shutdown event
If the startup event took longer, which step would be delayed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Startup event runs at step 1; app becomes ready at step 2 to handle requests
Concept Snapshot
FastAPI runs startup events before handling requests.
Shutdown events run before app stops.
Use @app.on_event("startup") and @app.on_event("shutdown") decorators.
Events can be async for async setup/cleanup.
Startup prepares app; shutdown cleans up resources.
Full Transcript
In FastAPI, startup and shutdown events let you run code when the app starts and stops. When the app starts, it runs the startup event first to prepare resources. Then it handles incoming requests. When the app receives a shutdown signal, it runs the shutdown event to clean up before stopping. You define these events using @app.on_event decorators with 'startup' or 'shutdown'. These functions can be asynchronous to handle async tasks. The execution table shows the app state changing from not started to ready after startup, then stopping after shutdown. This helps manage resources safely during app lifecycle.