How can we handle pre shutdown? #2646
-
Hello, Currently, when uvicorn's code receives a signal, the shutdown process begins immediately. This process executes To properly handle requests from a load balancer, I'd like to be able to trigger an event like For example, consider the health check implementation here: app = FastAPI()
is_shutting_down = False
def pre_shutdown_handler():
global is_shutting_down
is_shutting_down = True
@app.get("/health")
async def health():
global is_shutting_down
if is_shutting_down:
return Response(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
content="service unavailable",
)
return Response(status_code=status.HTTP_200_OK, content="ok")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"src.main:app",
host="0.0.0.0",
port=PORT,
log_level="info",
reload=True,
timeout_graceful_shutdown=10,
) Is it possible to trigger an event such as Thank you for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
There is no way other than patching. There is nothing in the ASGI spec that regulates that. |
Beta Was this translation helpful? Give feedback.
Instead of blocking sleep, you can delay the original handler call with
loop.call_later
: