I was able to get my flask app running as a service thanks to Is it possible to run a Python script as a service in Windows? If possible, how?, but when it comes to stopping
You can stop the Werkzeug web server gracefully before you stop the Win32 server. Example:
from flask import request
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
If you add this to your Flask server you can then request a graceful server shutdown by sending a POST request to /shutdown. You can use requests or urllib2 to do this. Depending on your situation you may need to protect this route against unauthorized access.
Once the server has stopped I think you will have to no problem stopping the Win32 service.
Note that the shutdown code above appears in this Flask snippet.