Copy flask request/app context to another process Copy flask request/app context to another process flask flask

Copy flask request/app context to another process


Not sure if this is suitable for you, but there is a possibility to use Flask application context from other processes. It requires you to utilize the application factory pattern.

# your_project/app.pydef create_app(config_file):    # Load the config file somehow    # Create your app instance    app = Flask(__name__)    # Initialize your extensions    db.init_app(app)    # Mount routes, etc    app.register_blueprint(...)    # Return your app    return app# your_project/wsgi.pyfrom .app import create_appapp = create_app("./config/local.cfg")

You define an application factory and use it to create your app as an entry point in wsgi.py or another convenient location, and you can later access this app instance from your background script which would run in another process.

# your_project/background_script.pyfrom .wsgi import appwith app.app_context():    # Do some stuff

BTW, this is also a way to use Flask with Celery, for example, as described in this question.Hope this helps you.