How to stop flask application without using ctrl-c
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):
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.get('/shutdown')
def shutdown():
shutdown_server()
return 'Server shutting down...'
Here is another approach that is more contained:
from multiprocessing import Process
server = Process(target=app.run)
server.start()
# ...
server.terminate()
server.join()
Let me know if this helps.
How to move out of app.run() in flask API Python
I have resolved the issue by starting the API server in separate thread so that Ui keeps on working. Then I am shutting down the API server using shutdown api
run = True
def start_api_server():
while run:
start_local_server()
time.sleep(1)
print("SERVER HAS STOPPED")
@pyqtSlot()
def on_click_start_btn(self):
Thread(target=start_api_server).start()
Above is the code to start the server on button click. Below is how I am shutting it down:
@pyqtSlot()
def on_click_stop_btn(self):
global run
print("STOPPING SERVER")
run = False
try:
x = requests.get("http://localhost:80/api/shutdown")
rdata = x.text
rdata = json.loads(rdata)
if rdata['status']:
print("Server shutting down")
print(rdata)
except Exception as e:
print(e)
Above code calls a shutdown API which terminate the flask api server. Below is the code for it:
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('/api/shutdown')
def shutdown():
shutdown_server()
return 'Server shutting down...'
How do I terminate a flask app that's running as a service?
I recommend you use http://supervisord.org/. Actually not work in Windows, but with Cygwin you can run supervisor as in Linux, including run as service.
For install Supervisord: https://stackoverflow.com/a/18032347/3380763
After install you must configure the app, here an example: http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/ (Is not necessary that you use Nginx with the Supervisor's configuration is enough)
Related Topics
Find the Item With Maximum Occurrences in a List
How to Stop a Running Function Without Exiting the Tkinter Window Entirely
Printing a Multiplication Table With Nested Loops
How to Compute the Gradients of Image Using Python
How to Test If an Enum Member With a Certain Name Exists
Using Look Up Tables in Python
Getting Value in a Dataframe in Pyspark
How to Find the Average Colour of an Image in Python With Opencv
Numpy Array Typeerror: Only Integer Scalar Arrays Can Be Converted to a Scalar Index
Move Seaborn Plot Legend to a Different Position
Python: Pandas Pd.Read_Excel Giving Importerror: Install Xlrd >= 0.9.0 for Excel Support
Python - Having Trouble Opening a File With Spaces
Convert a Tensor to Numpy Array in Tensorflow
How to End Program If Input == "Quit" With Many If Statements
How to Remove \N from a List Element
Sqlalchemy: How to Join Several Tables by One Query
What Does Sqlite3.Operationalerror: Near "-": Syntax Error Mean