计划 python 清除作业队列

Schedule python clear jobs queue

我正在尝试按如下方式使用时间表:

def job():
   my code

schedule.every().day.at("06:03").do(job)
schedule.every().day.at("09:56").do(job)

while True:
   schedule.run_pending()
   sleep(1)

我的工作用不了 1 到 10 个小时就可以完成。

我遇到的问题是:

基本上,当第一份工作 运行s(在 06:03)如果工作需要大约 10 个小时,当它结束时它会再次开始,因为计划 运行s 所有它丢失的作业(在本例中它错过了 09:56 的作业,因此它 运行 就是那个)。

但是我想要的是,如果作业需要很长时间,它丢失的时间表不会在之后立即 运行,但它必须从下一个时间表重新开始(在示例中06:03)。简单来说,如果错过了一个调度,需要清理“调度队列”,从下一个调度时间重新开始。所有错过的预定时间不需要是运行.

这里是您需要的准系统解决方案的示例,为了清楚起见,我尽量不进行优化。

是在结束前重新调度自身并清除现有实例的作业。

import time
import schedule

def job_every_nsec(seconds=10):
    ### Job code
    print("before_job_every_nsec", time.time()) 
    time.sleep(20) 
    print("after_job_every_nsec", time.time())
    ### End of job code 
    # after the code of your job schedule the same job again
    # notice that the functions calls itself (recursion)
    schedule.every(seconds).seconds.do(job_every_nsec, seconds=seconds)
    # and clear the existing one
    return schedule.CancelJob

def job_at(start_at):
    ### Job code
    print("before job_at", time.time()) 
    time.sleep(20) 
    print("after job_at", time.time())
    ### End of job code 
    # after the code of your job schedule the same job again
    schedule.every().day.at(start_at)
    # and clear the existing one
    return schedule.CancelJob


# launch jobs for the first time
schedule.every(10).seconds.do(job_every_nsec, seconds=10)
schedule.every().day.at("12:30").do(job_at, start_at="12:30")

while True:
    schedule.run_pending()
    time.sleep(1)