英文:
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 CronTrigger
s 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论