0

This is a follow-up to this question: How to stop flask application without using ctrl-c . The problem is that I didn't understand some of the terminology in the accepted answer since I'm totally new to this.

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Dash Tutorials'),
    dcc.Graph()
])

if __name__ == '__main__':
    app.run_server(debug=True)

How do I shut this down? My end goal is to run a plotly dashboard on a remote machine, but I'm testing it out on my local machine first.

I guess I'm supposed to "expose an endpoint" (have no idea what that means) via:

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...'

Where do I include the above code? Is it supposed to be included in the first block of code that I showed (i.e. the code that contains app.run_server command)? Is it supposed to be separate? And then what are the exact steps I need to take to shut down the server when I want?

Finally, are the steps to shut down the server the same whether I run the server on a local or remote machine?

Would really appreciate help!

user9343456
  • 351
  • 2
  • 11
  • Which operating system runs on the remote machine? – v25 May 15 '20 at 13:59
  • The remote machine runs some variant of Linux, though I'm afraid I don't know specifically. There's a yum.conf file in /etc if that's any help. As for the local machine, it's MacOS – user9343456 May 15 '20 at 14:09

2 Answers2

1

The method in the linked answer, werkzeug.server.shutdown, only works with the development server. Creating a view function, with an assigned URL ("exposing an endpoint") to implement this shutdown function is a convenience thing, which won't work when deployed with a WSGI server like gunicorn.

Maybe that creates more questions than it answers:

I suggest familiarising yourself with Flask's wsgi-standalone deployment docs.

And then probably the gunicorn deployment guide. The monitoring section has a number of different examples of service monitors, which you can use with gunicorn allowing you to run the app in the background, start on reboot, etc.

Ultimately, starting and stopping the WSGI server is the responsibility of the service monitor and logic to do this probably shouldn't be coded into your app.

v25
  • 7,096
  • 2
  • 20
  • 36
1

What works in both cases of

app.run_server(debug=True)

and

app.run_server(debug=False)

anywhere in the code is:

os.kill(os.getpid(), signal.SIGTERM)

(don't forget to import os and signal) SIGTERM should cause a clean exit of the application.

Jan
  • 11
  • 1
  • 1
    I have posted this solution, in case someone might what to point out why this sould not be done to gracefully terminate the application... – Jan Nov 09 '20 at 12:35