Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code after flask application has started

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.

like image 625
lynn Avatar asked Dec 14 '14 01:12

lynn


People also ask

How do I run an existing flask app in terminal?

To run the app outside of the VS Code debugger, use the following steps from a terminal: Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .

How do you run a function in flask?

Debug modeA Flask application is started by calling the run() method. However, while the application is under development, it should be restarted manually for each change in the code. To avoid this inconvenience, enable debug support. The server will then reload itself if the code changes.


2 Answers

If you need to execute some code after your flask application is started but strictly before the first request, not even be triggered by the execution of the first request as @app.before_first_request can handle, you should use Flask_Script, as CESCO said, but you could subclass the class Server and overwrite the __ call __ method, instead of overwriting the runserver command with @manager.command:

from flask import Flask from flask_script import Manager, Server  def custom_call():     #Your code     pass  class CustomServer(Server):     def __call__(self, app, *args, **kwargs):         custom_call()         #Hint: Here you could manipulate app         return Server.__call__(self, app, *args, **kwargs)  app = Flask(__name__) manager = Manager(app)  # Remeber to add the command to your Manager instance manager.add_command('runserver', CustomServer())  if __name__ == "__main__":     manager.run() 

This way you don't override default options of runserver command.

like image 76
Ismael Avatar answered Sep 19 '22 21:09

Ismael


I just did (in a main.py executed with python main.py):

with app.app_context():     from module import some_code     some_code()  def run():     from webapp import app     app.run(debug=True, use_reloader=False) 

This worked for me without the more comprehensive answers offered above. In my case some_code() is initializing a cache via flask_caching.Cache.

But it probably depends on what exactly some_code is doing...

like image 37
jtlz2 Avatar answered Sep 20 '22 21:09

jtlz2