-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shutting down the uvicorn server master from a FastAPI worker #1509
Comments
I imagine you solved your problem, so thanks for closing the issue 👍 |
@ocanty Could you consider sharing your solution ? I have exactly the same question... |
Likewise, I've the same question, any insight into the best way to do this programatically? |
I found a very dirty way : def crash(): My app is inside a docker container with auto restart |
Same... what's the solution here? :-) |
How about this? api = FastAPI()
def self_terminate():
time.sleep(1)
parent = psutil.Process(psutil.Process(os.getpid()).ppid())
parent.kill()
@api.post("/kill")
async def kill():
threading.Thread(target=self_terminate, daemon=True).start()
return {"success": True} I'm using psutil to get parent process and kill it. 👍🏽 This sends a signal to parent process. This worked for me in Windows. (Should work in Linux/MacOS but not sure) You can also send parent process a SIGINT to simulate Ctrl+C 😎 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Description
I want to kill the entire uvicorn server whenever one of my workers encounters an issue it cannot resolve.
I currently run fastapi inside the bundled Docker container recommended in the documentation, when my worker cannot connect to an upstream api because it is down/unavailable, I can call sys.quit(-1) to kill the worker with an error code, but this only causes uvicorn to restart the worker.
Is there any interface to kill the uvicorn master server and return an error code so that it kills my Docker container? The only alternative I can see is using health checks instead of killing the container
One solution I have found would be using os.kill to kill pid 1/sending a SIGTERM but I'm not sure if ASGI may have a procedure/interface for this rather than bluntly signalling the process. I have searched the uvicorn and Starlette docs and found nothing
The text was updated successfully, but these errors were encountered: