【问题标题】:How to fix APscheduler?如何修复APscheduler?
【发布时间】:2019-12-07 05:13:30
【问题描述】:

我正在尝试每 24 小时运行一次函数并更新数据库。在此之前,我已经阅读了 APscheduler 上的文档并尝试了一个简单的工作

def print_date_time():
  print(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))
def testing_schedule():
    scheduler = BackgroundScheduler()
    scheduler.add_job(func=print_date_time, trigger="interval", seconds=5)
    scheduler.start()

    pass

testing_schedule()

据我了解,每 5 秒 print_date_time 函数将运行打印当前时间并继续运行,直到我关闭它。

究竟是什么问题?

【问题讨论】:

  • 我没有安装 APscheduler,所以无法测试这个理论...但我注意到您在 test_schedule() 函数中创建的实例是一个 local 变量当函数返回时,它会自动被删除——所以怀疑这可能是问题的原因。

标签: python cron scheduled-tasks apscheduler


【解决方案1】:

在 APScheduler 中,您需要先启动调度程序,然后再添加作业。您还需要一个带有 BackGround 调度程序的 while 循环来保持进程处于活动状态。 所以:

scheduler.start()
scheduler.add_job(func=print_date_time, trigger="interval", seconds=5)
while True:
    time.sleep(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多