Uvicorn Graceful Shutdown Workers
Configure uvicorn worker shutdown with timeout windows, draining in-flight requests and cancelling background loops cleanly on signals.
Code
import asyncio
import contextlib
from fastapi import FastAPI
app = FastAPI()
shutdown_event = asyncio.Event()
@app.get('/health')
async def health():
if shutdown_event.is_set():
return {'status': 'draining'}
return {'status': 'ready'}
@app.on_event('shutdown')
async def release_resources():
print('draining in-flight requests')
await asyncio.sleep(0.2)
print('background loops cancelled')
async def background_sync():
while not shutdown_event.is_set():
await asyncio.sleep(5)
print('sync tick')
print('background loop exited cleanly')
@contextlib.asynccontextmanager
async def runner():
task = asyncio.create_task(background_sync())
try:
yield
finally:
shutdown_event.set()
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
if __name__ == '__main__':
# 关键在于命令行参数:
# uvicorn app:app --timeout-graceful-shutdown 20
# 收到 SIGTERM 后:停止接新请求 → 排空在途请求 → 最多等 20 秒 → 强制退出
import uvicorn
uvicorn.run(
'app:app',
host='0.0.0.0',
port=8000,
timeout_graceful_shutdown=20,
timeout_keep_alive=5,
)What the code does
Count the functions health, release_resources, background_sync and runner as the entire surface area of Uvicorn Graceful Shutdown Workers.
Repetition is confined to while not shutdown_event.is_set() — the rest of the file is straight-line. The asynchronous flow is linearized with await, keeping exception handling local. FastAPI are the only symbols borrowed from fastapi — the coupling is explicit and narrow.
A fresh value is allocated for the result; the arguments stay untouched.
What to watch for
- Dependency check:
fastapi,asyncioanduvicorn— not built-in, must be present in the project.
Applying it
The main signature reads def health()
The demo output is labeled (draining in-flight requests, background loops cancelled, sync tick), so every printed line maps back to its originating operation.
Statements that matter
return {'status': 'draining'}
return {'status': 'ready'}
with contextlib.suppress(asyncio.CancelledError):
await task
Interactive Tools for This Topic
FAQ
- Does `health` modify its input?
- No. Nothing passed in is written to, which makes the call safe on shared data.
- Are there third-party dependencies?
- `fastapi`, `asyncio` and `uvicorn` — install them with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.