+-
python – 安排一个函数在Flask上每小时运行一次
我有一个Flask web托管,无法访问cron命令.
我怎样才能每小时执行一些 Python函数?
最佳答案
您可以使用 BackgroundScheduler()包中的 BackgroundScheduler()(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处于调试模式时,将启动其中两个调度程序.有关更多信息,请查看this问题.

点击查看更多相关文章

转载注明原文:python – 安排一个函数在Flask上每小时运行一次 - 乐贴网