【问题标题】:How to run a method using scheduler after the flask app has started烧瓶应用程序启动后如何使用调度程序运行方法
【发布时间】:2020-08-20 02:18:27
【问题描述】:
@app.route('/page1', methods = ['POST'])
def func1():
    pass

@app.route('/page2', methods = ['POST'])
def func2():
    pass

def func3():
    pass

if __name__=="__main__":
    app.run(host='0.0.0.0', port=xxxx,debug=True)

如何安排运行 func3() 以便在烧瓶应用程序启动后触发它?

我使用过python schedule、Flask-APScheduler、APScheduler...该方法每隔一段时间运行一次,但是在flask应用程序启动后如何触发它?

【问题讨论】:

  • 应用程序是否必须完全加载,还是只是在调用app.run 之后运行一些代码?

标签: python flask scheduled-tasks apscheduler


【解决方案1】:

在 : 之后放置你的调度器。

if __name__ == '__main__': scheduler.func3()
app.run('localhost', 5000, debug=True)

但是,您需要声明希望调度程序运行的频率,因此您可以这样编写:

    scheduler.add_job(id='Third Function', func=func3, trigger="interval", seconds=40000)

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 2015-07-05
    • 2019-11-23
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多