Gunicorn--how to kill a worker if the client closes their connection?
Asked Answered
G

1

9

I've got a flask app running under gunicorn which handles client requests via REST api with an extremely CPU-intensive backend; some requests take minutes to respond to.

But that creates its own problem. If I, say, run a little script to make a request and kill it (ctrl-C or whatever), the flask app keeps on running despite the fact that no one will hear it when it comes back from the depths of computation and gets its broken pipe.

Is there a way to terminate the API call (even just kill/restart the worker) as soon as the client connection is broken? That feels like a thing Gunicorn could handle, but I'm powerless to find any setting that would do the trick.

Thanks--this has been vexing me!

Gilbreath answered 12/11, 2018 at 18:59 Comment(0)
A
-1

Killing a flask worker can be done with this code:

from flask import request
def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Werkzeug server doesn't run flask')
    func()
    
@app.route('/shutdown', methods=['GET'])
def shutdown():
    shutdown_server()
    return 'Shutting down...'

For killing a Gunicorn server on Linux, you can use this command, which I tested:

pkill gunicorn

This command works flawlessly on all kinds of Linuxes, which I assume you have installed for server

Or if I give you a Python implementation:

import os

def shutdownGunicorn():
    os.system("pkill gunicorn")

I don't think killing after request is done would be smart, because then you couldn't know when you will get next request.

Flask doesn't take much CPU and RAM usage while it's not working!

Hope that gives you an answer!

Autarchy answered 4/1, 2021 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.