Why is the Flask dev server launched twice?

I use Flask to develop a website, and during development I run the flask using the following file:

#!/usr/bin/env python from datetime import datetime from app import app import config if __name__ == '__main__': print '################### Restarting @', datetime.utcnow(), '###################' app.run(port=4004, debug=config.DEBUG, host='0.0.0.0') 

When I start the server or when it automatically restarts due to the files being updated, it always shows the print line twice:

 ################### Restarting @ 2014-08-26 10:51:49.167062 ################### ################### Restarting @ 2014-08-26 10:51:49.607096 ################### 

Although this is actually not a problem (everything else works as expected), I'm just wondering why it behaves this way? Any ideas?

+77
python flask
Aug 26 '14 at 10:58
source share
5 answers

Reloader Werkzeug spawns a child process so that it can restart this process every time your code changes. Werkzeug is a library that supplies Flask with a development server when calling app.run() .

See the function code restart_with_reloader() ; your script runs again with subprocess.call() .

If you set use_reloader to False , you will see that the behavior disappears, but you also lose the reload function:

 app.run(port=4004, debug=config.DEBUG, host='0.0.0.0', use_reloader=False) 

You can also disable the overloader when using the flask run command:

 FLASK_DEBUG=1 flask run --no-reload 

You can find the WERKZEUG_RUN_MAIN environment variable if you want to determine when you are in the process of reloading the child process:

 import os if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': print '################### Restarting @ {} ###################'.format( datetime.utcnow()) 

However, if you need to configure the module global variables, you should instead use the @app.before_first_request decorator for the function, and this function should set such global variables. It will be called once after each reboot when the first request arrives:

 @app.before_first_request def before_first_request(): print '########### Restarted, first request @ {} ############'.format( datetime.utcnow()) 

Note that if you run this on a full-blown WSGI server that uses branching or new subprocesses to process requests, before_first_request handlers can be called for each new subprocess.

+113
Aug 26 '14 at 11:00
source share

If you use the modern flask run command, none of the app.run options are used. To completely disable the reboot, go --no-reload :

 FLASK_DEBUG=1 flask run --no-reload 

In addition, __name__ == '__main__' will never be true, because the application is not running directly. Use the same ideas from Martijn's answer , with the exception of the __main__ block.

 if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': # do something only once, before the reloader if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': # do something each reload 
+9
Nov 12 '17 at 14:49
source share

I had the same problem and solved it by setting app.debug to False . Setting it to True caused my call __name__ == "__main__" twice.

(I would post it as a comment, but I didn’t have enough reputation. Added as an answer in the hope that it will help someone else)

+6
Dec 14 '16 at 18:47
source share

I had the same problem. I solved this by changing my main and inserting use_reloader = False into it. If any body is looking for a workaround for this problem here, then the code below will help you get started, however, the functionality of changes in the code that are automatically detected and restarting the application will not work. You will have to manually stop and restart your application after each editing in the code.

 if __name__ == '__main__': app.run(debug=True,use_reloader=False) 
0
24 Oct '18 at 13:06
source share

One possible reason Flask runs twice is to configure the WEB_CONCURRENCY parameter in Heroku. To install in one, you can write in the console heroku config:set WEB_CONCURRENCY=1

0
Sep 20 '19 at 12:22
source share



All Articles