【问题标题】:How to Schedule Python Code To Run Daily Using Apschedulers?如何使用 Apscheduler 安排 Python 代码每天运行?
【发布时间】:2021-03-02 11:18:36
【问题描述】:

我正在尝试使用 Apscheduler 每天运行代码但我遇到了一些问题 这是代码:

async def job():
      print("Boss Wake Up")


scheduler = AsyncIOScheduler()
scheduler.add_job(job, "cron", day_of_week="mon-sun", hour=21, minute=10)
scheduler.start()

但是从今天开始它就不能工作了。为什么?我想让它从第一天开始,每天运行。谁能帮我 ?谢谢!

【问题讨论】:

  • mon-sun 涵盖一周中的每一天,因此它是多余的(就像您不必指定它应该每年或每月运行一样)。

标签: python cron scheduler apscheduler


【解决方案1】:

你可以试试下面的

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger

sched = BlockingScheduler()

@sched.scheduled_job(IntervalTrigger(seconds=10))  #set the interval you need
def timed_job():
    print('This job is run every 10 seconds.')

sched.start()

如果你想在后台运行,你可以使用 BackgroundScheduler()

【讨论】:

    【解决方案2】:

    您仍然需要运行 asyncio 事件循环。添加以下块:

    try:
        asyncio.get_event_loop().run_forever()
    except (KeyboardInterrupt, SystemExit):
        pass
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多