Run code after flask application has started

后端 未结 6 928
渐次进展
渐次进展 2020-12-09 14:54

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(debu         


        
6条回答
  •  鱼传尺愫
    2020-12-09 15:16

    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...

提交回复
热议问题