How to properly close a background child process in a shutdown event?
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
Here’s a self-contained, minimal, reproducible, example with my use case:
from multiprocessing import Process, Value
from fastapi import FastAPI
app = FastAPI()
terminate_flag = Value('h', 0)
def work_hard(terminate_flag: Value):
while terminate_flag.value == 0:
# CPU-bound calculations
pass
@app.get('/hello-world')
def hello_world():
return {
'message': 'hello world'
}
process = Process(target=work_hard, args=(terminate_flag,))
@app.on_event('startup')
def startup_event():
process.start()
@app.on_event('shutdown')
def shutdown_event():
terminate_flag.value = 1
process.join()
Description
I have some CPU-bound calculations known as work_hard
, which contains a flagged loop to control its flow.
The function is designed to be executed in a child process that starts within FastAPI startup
event, and joins within FastAPI shutdown
event.
However, it seems that the FastAPI shutdown
event does not get executed while uvicorn
is shutting down by the user pressing Ctrl + C.
It stuck at the log “Finished server process
” and never really terminates.
I did try to look up for the standard practice of using multiprocessing
module in FastAPI, but there are no concrete examples about it.
Concurrency and async/await section mentioned that “To see how to achieve this parallelism in production see the section about Deployment.”, yet Deployment section has no such examples as well.
Thank you in advance!
Environment
- OS: Ubuntu 18.04.4 TLS
- FastAPI Version : 0.61.1
- Python version: 3.6.8
Additional context
cliff@cliff-pc:~/repo/fastapi-multiprocessing$ uvicorn app.main:app
INFO: Started server process [24032]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
^CINFO: Shutting down
INFO: Finished server process [24032]
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (8 by maintainers)
Top GitHub Comments
Also to respond to your question :
FastAPI shutdown event does not get executed while uvicorn is shutting down by the user pressing Ctrl + C.
No it does
give
Probably you run the code inside a docker and do not use tini
Thanks for the help and discussion here everyone and thanks @clifflau1120 for coming back to close the issue. ☕