Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload python flask server by function

I'm writing a python/flask application and would like to add the functionality of reloading the server.

I'm currently running the server with the following option

app.run(debug=True)

which results in the following, each time a code change happens

* Running on http://127.0.0.1:5000/
* Restarting with reloader

In a production environment however, I would rather not have debug=True set, but be able to only reload the application server whenever I need to.

I'm trying to get two things working:

  1. if reload_needed: reload_server(), and
  2. if a user clicks on a "Reload Server" button in the admin panel, the reload_server() function should be called.

However, despite the fact that the server get's reloaded after code changes, I couldn't find a function that let's me do exactly that.

If possible I would like to use the flask/werkzeug internal capabilities. I am aware that I could achieve something like that by adding things like gunicorn/nginx/apache, etc.

like image 267
mohrphium Avatar asked Dec 31 '14 15:12

mohrphium


People also ask

How do you refresh a Flask?

The SolutionUse the --debug option when you run your Flask app to enable debug mode. This will enable auto-reload whenever changes are made to your code and saved. It will also enable an interactive debugger in the browser if any errors occur during a request.

How do I rerun Flask app?

To run the application, use the flask command or python -m flask . You need to tell the Flask where your application is with the --app option. As a shortcut, if the file is named app.py or wsgi.py , you don't have to use --app . See Command Line Interface for more details.

Does Flask have hot reload?

By default, flask run launches a production-friendly server process. However, you can opt into a hot-reloading debug mode by setting the FLASK_ENV environment variable. Now, changing any python source files will automatically restart the flask process.


1 Answers

I think I've had the same problem.

So there was a python/flask application (XY.py), on clients. I wrote a build step (Teamcity) which deploys this python code to the clients. Let's suppose the XY.py is already running on the clients. After deploying this new/fixed/corrected XY.py I had to restart it for applying the changes on the running code.

The problem what I've had is that after using the fine restarting oneliner os.execl(sys.executable, *([sys.executable]+sys.argv)) my port used by app is still busy/established, so after restarting I can't reach it.

This is how I resolved the problem: I put my app to run on a separate Process and made a queue for it. To see it more cleanly here is some code.

global some_queue = None

@app.route('/restart')
def restart():
   try:
     some_queue.put("something")
     return "Quit"

def start_flaskapp(queue):
   some_queue = queue
   app.run(your_parameters)

Add this to your main:

q = Queue()
p = Process(target=start_flaskapp, args=[q,])
p.start()
while True: #wathing queue, sleep if there is no call, otherwise break
   if q.empty(): 
        time.sleep(1)
   else:
      break
p.terminate() #terminate flaskapp and then restart the app on subprocess
args = [sys.executable] + [sys.argv[0]]
subprocess.call(args)

Hope it was clean and short enough and it helped to you!

like image 60
tamasbalassa Avatar answered Oct 13 '22 02:10

tamasbalassa