How to gracefully stop FastAPI app ?

See original GitHub issue

Hi All,

I just learn FastAPI and here is a very basic question raised here: How to gracefully stop FastAPI app ? We know “graceful stop” is a very critical concept in the modern web application. “Graceful stop” could help us avoid losing any request/data on the fly if we restart or stop apps, especially in rolling update. Many other frameworks, such as Go and Springboot consists of the solution to support “graceful stop”. But I searched around the Forum and Google but I didn’t find in detail how to do it in FastAPI app. I just notice there is a parameter “graceful_timeout” in “gunicorn” from https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#graceful_timeout . But I am starting FastAPI by the following “uvicorn” instead of “gunicorn” command in my own docker (not official uvicorn-gunicorn-fastapi-docker):

uvicorn.run("app:app",host='0.0.0.0', port=8000, reload=True, debug=True, workers=3)

So I am not sure whether graceful stop is also supported in “uvicorn”. Is there a general solution we could gracefully stop FastAPI APP ?

Thank you !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
raphaelauvcommented, Apr 17, 2021

Nobody put --reload on production ( dockerize or not ) deployment . Since it’s not a fastAPI issue, I propose that you close the issue.

3reactions
Halkcyoncommented, Mar 12, 2021

It might be helpful to point out that uvicorn recommends using gunicorn with a uvicorn worker in a production deployment (aka, not localhost)

https://www.uvicorn.org/deployment/#gunicorn

$ gunicorn -w 4 -k uvicorn.workers.UvicornWorker

I personally use a gunicorn configuration file instead of the cli (note: 20.1.x is not on pypi yet, so I’ve pinned this doc to its latest release)

https://docs.gunicorn.org/en/20.0.4/configure.html#configuration-file

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events: startup - shutdown - FastAPI
Only event handlers for the main application will be executed, ... be run when the application is shutting down, declare it with the...
Read more >
Tips and Tricks - FastAPI shutdown events - TestDriven.io
FastAPI shutdown events. FastAPI tip: You can register functions to run before the application shutdown using @app.on_event("shutdown") .
Read more >
How to terminate a Uvicorn + FastAPI application cleanly with ...
I want to terminate the application process cleanly at the end of the test. Do you have any idea? The details are as...
Read more >
How to gracefully stop FastAPI app - Bountysource
Hi All,. I just learn FastAPI and here is a very basic question raised here: How to gracefully stop FastAPI app ? We...
Read more >
tiangolo/fastapi - Gitter
Event() def shutdown(self): self.cancellation_event.set() async def ... asyncio import logging import uvicorn from fastapi import FastAPI app = FastAPI() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found