My goal is to get arbitrary code to run after my Flask application is started. Here is what I've got:
def run():
from webapp import app
app.run(debug=True, use_reloader=False)
Ideally I would be able to just do this:
def run():
from webapp import app
app.run(debug=True, use_reloader=False)
some_code()
But the code doesn't continue past app.run()
, so some_code() never runs.
The solution I'm working on at the moment is to run some_code() in a separate thread from app.run(), create a before first request function that sets this:
app.is_running = True
Then get some_code() to shoot a basic request to app so that the 'before first request' code runs. This is fairly convoluted and going to be hard to document. I would rather use app.is_running parameter which already is provided in Flask, or use a @app.after_server_start
decorator, but to my knowledge neither of those exists.
Help me make this code better?
Posthumous: Every time I think about this issue, it makes me wish that a @app.after_server_start
decorator existed.
@app.before_first_request
and be done with that. Why does the code need to run any earlier? – Mauriliapp.run()
) but before a request has come in? – Maurilibefore_first_request
then. – Maurili