How to gracefully stop FastAPI app ?
See original GitHub issueHi 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:
- Created 3 years ago
- Reactions:5
- Comments:9 (5 by maintainers)
Top Related StackOverflow Question
Nobody put --reload on production ( dockerize or not ) deployment . Since it’s not a fastAPI issue, I propose that you close the issue.
It might be helpful to point out that
uvicornrecommends usinggunicornwith auvicornworker in a production deployment (aka, not localhost)https://www.uvicorn.org/deployment/#gunicorn
I personally use a
gunicornconfiguration 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