vrootic vrootic - 1 year ago 930
Python Question

How to stop flask application without using ctrl-c

I want to implement a command which can stop flask application by using flask-script.
I have searched the solution for a while. Because the framework doesn't provide "app.stop()" API, I am curious about how to code this. I am working on Ubuntu 12.10 and Python 2.7.3.

Answer Source

If you are just running the server on your desktop, you can expose an endpoint to kill the server (read more at Shutdown The Simple Server):

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')

@app.route('/shutdown', methods=['POST'])
def shutdown():
    return 'Server shutting down...'

Here is another approach that is more contained:

from multiprocessing import Process

def run_server():

server = Process(target=app.run)
# ...

Let me know if this helps.