如何安排一个函数在Flask上每小时运行一次? [英] How to schedule a function to run every hour on Flask?
本文介绍了如何安排一个函数在Flask上每小时运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Flask虚拟主机,无法访问cron
命令.
I have a Flask web hosting with no access to cron
command.
我如何每小时执行一些Python函数?
How can I execute some Python function every hour?
推荐答案
您可以使用 APScheduler 程序包(v3.5.3)的noreferrer> BackgroundScheduler()
:
You can use BackgroundScheduler()
from APScheduler package (v3.5.3):
import time
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
def print_date_time():
print(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))
scheduler = BackgroundScheduler()
scheduler.add_job(func=print_date_time, trigger="interval", seconds=3)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
请注意,当Flask处于调试模式时,将启动其中两个调度程序.有关更多信息,请查看此问题.
Note that two of these schedulers will be launched when Flask is in debug mode. For more information, check out this question.
这篇关于如何安排一个函数在Flask上每小时运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文