1from flask import Flask, request, jsonify
2
3# Workaround - otherwise doesn't work in windows service.
4cli = sys.modules['flask.cli']
5cli.show_server_banner = lambda *x: None
6
7app = Flask('MyService')
8
9# ... business logic endpoints are skipped.
10
11@app.route("/shutdown", methods=['GET'])
12def shutdown():
13 shutdown_func = request.environ.get('werkzeug.server.shutdown')
14 if shutdown_func is None:
15 raise RuntimeError('Not running werkzeug')
16 shutdown_func()
17 return "Shutting down..."
18
19
20def start():
21 app.run(host='0.0.0.0', threaded=True, port=5001)
22
23
24def stop():
25 import requests
26 resp = requests.get('http://localhost:5001/shutdown')
27
1from multiprocessing import Process
2
3server = Process(target=app.run)
4server.start()
5# ...
6server.terminate()
7server.join()
8