Challenge - 5 Problems
FastAPI Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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"}
Attempts:
2 left
💡 Hint
Think about when startup events run in FastAPI lifecycle.
✗ Incorrect
Startup event handlers run once when the app starts, before handling any requests. They can be async functions.
❓ component_behavior
intermediate2: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")
Attempts:
2 left
💡 Hint
Multiple shutdown handlers can be registered and all run on shutdown.
✗ Incorrect
FastAPI runs all registered shutdown event handlers on app shutdown. The order is not guaranteed.
📝 Syntax
advanced2: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")
Attempts:
2 left
💡 Hint
Check function definition syntax and async keyword.
✗ Incorrect
Option A correctly defines an async function with proper syntax. Others have syntax errors like missing colon or indentation.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
Consider the order and increments during startup and shutdown.
✗ Incorrect
Counter starts at 0, increments by 1 on startup, then by 2 on shutdown, total 3.
🔧 Debug
expert3: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"}
Attempts:
2 left
💡 Hint
Think about how FastAPI apps are run and how shutdown events are triggered.
✗ Incorrect
Shutdown events only run if the server supports lifespan events, e.g., uvicorn with proper shutdown. Running app without server or with improper server prevents shutdown events.