how to stop a flask server code example
Example 1: how to close a flask web server with python
from multiprocessing import Process
server = Process(target=app.run)
server.start()
server.terminate()
server.join()
Example 2: how to close a flask web server with python
from flask import Flask, request, jsonify
cli = sys.modules['flask.cli']
cli.show_server_banner = lambda *x: None
app = Flask('MyService')
@app.route("/shutdown", methods=['GET'])
def shutdown():
shutdown_func = request.environ.get('werkzeug.server.shutdown')
if shutdown_func is None:
raise RuntimeError('Not running werkzeug')
shutdown_func()
return "Shutting down..."
def start():
app.run(host='0.0.0.0', threaded=True, port=5001)
def stop():
import requests
resp = requests.get('http://localhost:5001/shutdown')