Execute code on Flask startup
By its dubious design Flask executes web server code twice during startup in debug mode, and just one time in the regular mode. Thus, to execute some code on startup, we need to distinct the first run from the second one which can be done analyzing environment variable
WERKZEUG_RUN_MAIN(applicable for debug mode only).
app = Flask(...) def is_flask_starting(app): wrm = (os.environ.get("WERKZEUG_RUN_MAIN") == 'true') return (not app.debug) or wrm if is_flask_starting(app): print('do smth on startup')