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.
Martijn Pieters Aug 26 '14 at 11:00 2014-08-26 11:00
source share