安排python清除作业队列

安排python清除作业队列

本文介绍了安排python清除作业队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我的工作不能在 1 到 10 个小时内完成执行.

My job can’t take from 1 to 10 hours to finish executing.

我遇到的问题是:

基本上,当第一个作业运行时(在 06:03),如果作业需要大约 10 个小时,那么在它结束时它会再次启动,因为 schedule 会运行它丢失的所有作业(在这种情况下它错过了作业在 09:56,因此它运行).

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).简单的说,如果错过了某个调度,就需要清理调度队列",从下一个调度时间重新开始.所有错过的预定时间都不需要跑.

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

这篇关于安排python清除作业队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:11