简体   繁体   English

如何使用 Apschedulers 安排 Python 代码每天运行?

[英]How to Schedule Python Code To Run Daily Using Apschedulers?

I am trying to use Apschedulers to Run a code Daily But i am facing some issue Here is Code :我正在尝试使用 Apschedulers 每天运行代码但我面临一些问题这里是代码:

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()

But its not working starting from today.但是从今天开始就不行了。 why?为什么? i wanna make it start from day one and run daily.我想让它从第一天开始,每天运行。 Can anyone help me ?谁能帮我 ? Thanks !谢谢 !

Can you try the following你可以试试下面的

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()

You can use BackgroundScheduler() if you want to run this in background如果你想在后台运行它,你可以使用 BackgroundScheduler()

You still need to run the asyncio event loop.您仍然需要运行 asyncio 事件循环。 Add the following block:添加以下块:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM