在APScheduler中安排工作在工作日的两个时间之间。

huangapple go评论65阅读模式
英文:

Scheduling job in APScheduler weekdays between 2 times

问题

我想安排一个工作,从周一到周五,在两个指定的时间之间运行,比如10:20到18:35,每5分钟运行一次。这样它会在每个工作日运行:10:25,10:30,10:35,...,18:30,18:35。我尝试结合CronTrigger和IntervalTrigger,但无法成功。有人能帮忙吗?

英文:

I want to schedule a job mon-fri, between two given times, say 10:20 to 18:35, for every 5 minute.
So it will run: 10:25, 10:30, 10:35, ..., 18:30, 18:35. every weekday.
I tried combining CronTrigger with IntervalTrigger but wasn't able to make it. Can anyone help?

答案1

得分: 0

我使用多个 CronTrigger 结合 OrTrigger 完成了这个任务。

OrTrigger([
    CronTrigger(day_of_week='mon-fri', hour='10', minute='25-59/5'),
    CronTrigger(day_of_week='mon-fri', hour='11-17', minute='*/5'),
    CronTrigger(day_of_week='mon-fri', hour='18', minute='0-35/5')
])

第一个 CronTrigger 是为起始小时(即10)设置的,调整分钟,每5分钟运行一次。中间的触发器在17点之前每5分钟运行一次。最后一个是在最后一个小时内,每5分钟运行一次。这个解决方案可以推广到任何起始时间、结束时间和间隔。

英文:

I was able to do this using combining multiple CronTriggers using an OrTrigger.

OrTrigger([
    CronTrigger(day_of_week='mon-fri', hour='10', minute='25-59/5'),
    CronTrigger(day_of_week='mon-fri', hour='11-17', minute='*/5'),
    CronTrigger(day_of_week='mon-fri', hour='18', minute='0-35/5')
])

The first CronTrigger is for the starting hour (i.e. 10), adjusted the minutes to run only from the 25th minute, every 5 minutes. The middle trigger runs every 5 minutes till the 17th hour. The last one is for the last hour, till the 35th minute.
This solution can be generalized to any start time, end time, and interval.

huangapple
  • 本文由 发表于 2023年5月11日 15:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定