Bird
0
0

Which of the following is the correct syntax to define a shutdown event handler in FastAPI?

easy📝 Syntax Q12 of 15
FastAPI - Middleware and Hooks
Which of the following is the correct syntax to define a shutdown event handler in FastAPI?
A@app.on_event("start") def cleanup(): print("Cleaning up")
B@app.shutdown_event def cleanup(): print("Cleaning up")
C@app.on_event("shutdown") def cleanup(): print("Cleaning up")
D@app.event("shutdown") def cleanup(): print("Cleaning up")
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct decorator for shutdown

    The correct decorator is @app.on_event("shutdown") to run code when the app stops.
  2. Step 2: Check syntax correctness

    Options A, B, and D use incorrect decorator names or syntax.
  3. Final Answer:

    @app.on_event("shutdown")\ndef cleanup():\n print("Cleaning up") -> Option C
  4. Quick Check:

    Shutdown event decorator = @app.on_event("shutdown") [OK]
Quick Trick: Shutdown event uses @app.on_event("shutdown") exactly [OK]
Common Mistakes:
MISTAKES
  • Using wrong event name like "start" instead of "shutdown"
  • Missing @app.on_event decorator
  • Using non-existent decorators like @app.shutdown_event

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes