安排python清除作业队列

编程入门 行业动态 更新时间:2024-10-27 08:32:08
本文介绍了安排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清除作业队列

本文发布于:2023-11-24 08:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   队列   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!