Run code after flask application has started

后端 未结 6 919
渐次进展
渐次进展 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:19

    I encountered this same issue in a flask app of mine. I wanted to start a scheduler at app startup, which would kick off some jobs at regular intervals. Since I deploy my apps inside docker containers, what I ended up doing is adding an "health check" endpoint which just returns a 200, and in my dockerfile configured that endpoint:

    HEALTHCHECK CMD curl --fail http://localhost:8080/alive || exit 1

    The default behavior is to execute that command every 30s, and the first run conveniently kicks off my init() method. https://docs.docker.com/engine/reference/builder/#healthcheck

提交回复
热议问题