Bird
0
0

What is the main purpose of using @app.on_event("startup") in a FastAPI application?

easy🧠 Conceptual Q11 of 15
FastAPI - Middleware and Hooks
What is the main purpose of using @app.on_event("startup") in a FastAPI application?
ATo define API routes for the application.
BTo handle HTTP requests from clients.
CTo shut down the server immediately.
DTo run code when the application starts, like initializing resources.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the startup event role

    The @app.on_event("startup") decorator marks a function to run when the app starts, useful for setup tasks.
  2. Step 2: Differentiate from other app parts

    Handling requests or shutting down are different concerns; startup is specifically for initialization.
  3. Final Answer:

    To run code when the application starts, like initializing resources. -> Option D
  4. Quick Check:

    Startup event = run code at app start [OK]
Quick Trick: Startup event runs code once when app launches [OK]
Common Mistakes:
MISTAKES
  • Confusing startup with request handling
  • Thinking startup runs multiple times per request
  • Mixing startup with shutdown event

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes