0
0
FastAPIframework~20 mins

Startup and shutdown events in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the FastAPI app starts?
Consider this FastAPI app with a startup event handler. What will be printed to the console when the app starts?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    print("App is starting")

@app.get("/")
async def read_root():
    return {"message": "Hello"}
ANothing is printed until the first request is received
BThe app raises an error because startup events must be synchronous
CThe message 'App is starting' is printed every time a request is received
DThe message 'App is starting' is printed once when the app starts
Attempts:
2 left
💡 Hint
Think about when startup events run in FastAPI lifecycle.
component_behavior
intermediate
2:00remaining
What happens on app shutdown with multiple handlers?
Given this FastAPI app with two shutdown event handlers, what will be printed when the app shuts down?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.on_event("shutdown")
async def shutdown_event_1():
    print("Shutdown handler 1")

@app.on_event("shutdown")
async def shutdown_event_2():
    print("Shutdown handler 2")
ABoth 'Shutdown handler 1' and 'Shutdown handler 2' are printed, order not guaranteed
BOnly 'Shutdown handler 2' is printed
CNo output is printed because shutdown handlers must be synchronous
DOnly 'Shutdown handler 1' is printed
Attempts:
2 left
💡 Hint
Multiple shutdown handlers can be registered and all run on shutdown.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this startup event handler
Which option shows the correct way to define a startup event handler in FastAPI without syntax errors?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.on_event("startup")
def startup_event():
    print("Starting")
A
@app.on_event("startup")
async def startup_event():
    print("Starting")
B
@app.on_event("startup")
def startup_event
    print("Starting")
C
@app.on_event("startup")
async startup_event():
    print("Starting")
D
@app.on_event("startup")
def startup_event():
print("Starting")
Attempts:
2 left
💡 Hint
Check function definition syntax and async keyword.
state_output
advanced
2:00remaining
What is the value of the counter after app shutdown?
This FastAPI app increments a counter on startup and shutdown events. What is the final value of counter after the app starts and then shuts down?
FastAPI
from fastapi import FastAPI

app = FastAPI()
counter = 0

@app.on_event("startup")
async def startup_event():
    global counter
    counter += 1

@app.on_event("shutdown")
async def shutdown_event():
    global counter
    counter += 2
A0
B3
C2
D1
Attempts:
2 left
💡 Hint
Consider the order and increments during startup and shutdown.
🔧 Debug
expert
3:00remaining
Why does this shutdown event handler not run?
This FastAPI app has a shutdown event handler but it never prints its message when the app stops. Why?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.on_event("shutdown")
def shutdown_event():
    print("App is shutting down")

@app.get("/")
async def read_root():
    return {"msg": "Hello"}
AThe shutdown event handler is missing the @app.shutdown decorator
BThe shutdown event handler is synchronous but must be async to run
CThe app must be run with a server that supports lifespan events for shutdown to trigger
DThe shutdown event handler must be called manually inside a route
Attempts:
2 left
💡 Hint
Think about how FastAPI apps are run and how shutdown events are triggered.