Python / Uvicorn Fundamentals Interview Questions
How do you configure Uvicorn's timeout settings?
Uvicorn provides several timeout parameters that control how long the server waits for client activity and worker health before taking action.
| Setting | Default | Purpose |
|---|---|---|
| --timeout-keep-alive | 5s | Seconds to wait for a new request on a keep-alive connection before closing |
| --timeout-notify | 30s | Seconds to notify worker processes during graceful shutdown before sending SIGKILL |
| --timeout-graceful-shutdown | None (no limit) | Maximum seconds for the server to wait for ongoing requests to complete during shutdown |
| --timeout-worker-healthcheck | 5s | Seconds to wait for a worker to respond to a health check |
# Configure keep-alive timeout (how long idle connections stay open) uvicorn main:app --timeout-keep-alive 10 # Graceful shutdown timeout (force-kill after N seconds if requests don't finish) uvicorn main:app --timeout-graceful-shutdown 30 # Worker health check timeout uvicorn main:app --timeout-worker-healthcheck 10 # Programmatic: uvicorn.run( "main:app", timeout_keep_alive=10, timeout_graceful_shutdown=30, ) # Gunicorn has its own timeout settings that overlay Uvicorn's: # gunicorn --timeout 120 (worker timeout for handling a request) # gunicorn --graceful-timeout 30 (graceful shutdown window)
Keep-alive tuning: the default 5-second keep-alive timeout is appropriate for most use cases. For high-throughput APIs with many short-lived requests, a lower value (2-3s) reduces idle connections. For APIs with slower clients or large payloads, a higher value reduces connection setup overhead.
More Related questions...