首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在特定的时间和间隔内调度作业?

如何在特定的时间和间隔内调度作业?
EN

Stack Overflow用户
提问于 2018-09-06 14:53:42
回答 2查看 1.9K关注 0票数 1

使用python的调度模块,我如何在特定时间启动作业,并在此基础上定期调度作业。

假设我想要从上午09:00开始每4小时安排一次任务

schedule.every(4).hours.at("09:00").do(task) # This doesn't work

如何实现上述目标?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-06 15:42:29

您可以将内部日程表(每4小时)转换为一个单独的函数,该函数将由主日程表(固定时间)调用。内部调度函数将调用您的作业函数。

示例:

代码语言:javascript
复制
import schedule
import time

def job():
    print "I am working" #your job function

def schedule_every_four_hours():
    job() #for the first job to run
    schedule.every(4).hour.do(job)
    return schedule.CancelJob

schedule.every().day.at("09:00").do(schedule_every_four_hours)

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

如果你想终止基于你的需求的时间表,请在这里阅读更多。Check here

票数 2
EN

Stack Overflow用户

发布于 2018-09-07 17:00:58

如果有多个调度,上述解决方案将不起作用,因为schedule.CancelJob会取消管道上的其他调度,最好使用clear标签

代码语言:javascript
复制
import schedule
from datetime import  datetime
import time
def task():
    print 'I am here...',datetime.now()

def schedule_every_four_hours(clear):
    if clear =='clear':
        schedule.every(2).seconds.do(task).tag('mytask1') #for the first job to runschedule.every(4).hour.at("9:00").do(task)
    else:
        schedule.every(5).seconds.do(task).tag('mytask2')  # for the second job to runschedule.every(4).hour.at("9:00").do(task)
    print clear
    schedule.clear(clear)

now = datetime.now()


times = str(now.hour+0)+ ":"+str(now.minute+1)

times1 = str(now.hour+0)+ ":"+str(now.minute+3)
schedule.every().day.at(times).do(schedule_every_four_hours,'clear').tag('clear')


schedule.every().day.at(times1).do(schedule_every_four_hours,'clear1').tag('clear1')



while True:
    schedule.run_pending()
    time.sleep(1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52198116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档