Performance: Startup and shutdown events
MEDIUM IMPACT
This concept affects the initial server readiness time and graceful shutdown, impacting how fast the app starts serving requests and releases resources.
import asyncio from fastapi import FastAPI app = FastAPI() @app.on_event("startup") async def startup_event(): await asyncio.sleep(0) # Non-blocking placeholder print("App started")
from fastapi import FastAPI app = FastAPI() @app.on_event("startup") def startup_event(): import time time.sleep(5) # Blocking delay print("App started")
| Pattern | Blocking Behavior | Startup Delay | Shutdown Delay | Verdict |
|---|---|---|---|---|
| Blocking synchronous startup code | Blocks event loop | Delays startup by seconds | N/A | [X] Bad |
| Non-blocking async startup code | Non-blocking | Immediate startup | N/A | [OK] Good |
| No resource cleanup on shutdown | N/A | N/A | Prolonged shutdown, leaks | [X] Bad |
| Async resource cleanup on shutdown | Non-blocking | N/A | Fast, clean shutdown | [OK] Good |