简体   繁体   English

计划 python 清除作业队列

[英]Schedule python clear jobs queue

I'm a trying to use schedule as follows:我正在尝试使用如下时间表:

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)

My job can't take from 1 to 10 hours to finish executing.我的工作不能花 1 到 10 个小时来完成执行。

The problem that I'm having is this:我遇到的问题是:

Basically, when the first job runs (at 06:03) if the job takes around 10 hours, right when it ends it starts again because schedule runs all the jobs that it has been missing (in this case it missed the job at 09:56, and therefore it runs that).基本上,当第一个作业运行时(在 06:03),如果作业需要大约 10 个小时,那么当它结束时它会再次开始,因为 schedule 会运行它丢失的所有作业(在这种情况下,它错过了 09 的作业: 56,因此它运行)。

However what I want is that if the job takes very long the schedule that it has been missing doesn't run right after, but it has to start again from the next schedule (in the example at 06:03).但是我想要的是,如果作业需要很长时间,那么它丢失的时间表不会立即运行,而是必须从下一个时间表重新开始(在示例中为 06:03)。 Simply put, if a schedule is missed, the “scheduler queue “ needs to be cleaned and start again from the next scheduled time.简单来说,如果错过了一个调度,就需要清理“调度队列”,从下一个调度的时间重新开始。 All the scheduled times missed don't need to be ran.不需要运行所有错过的预定时间。

Here an example of a barebone solution for what you need, I try not to optimize to be clear.这是您需要的准系统解决方案示例,我尽量不进行优化以使其清晰。

It Is a job that just before the end reschedules itself and clears the existing instance.这是一项在结束之前重新安排自身并清除现有实例的工作。

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)

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

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