使用Python APS顺序运行挂起的任务 [英] Sequentially run pending tasks with Python APS
本文介绍了使用Python APS顺序运行挂起的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有两个cron触发器:
trigger1 = CronTrigger(second='0,20,40')
trigger2 = CronTrigger(second='0,10,20,30,40,50')
我这样创建我的计划程序:
scheduler = BlockingScheduler()
scheduler.add_job(lambda: method1(param1, param2), trigger=trigger1)
scheduler.add_job(lambda: method2(param1, param3), trigger=trigger2)
使用这两种有效的方法:
def method1(s, t):
print("doing work in method1")
time.sleep(2)
print("doing work in method1")
time.sleep(2)
print("doing work in method1")
time.sleep(2)
def method2(s, t):
print("doing work in method2")
time.sleep(2)
print("doing work in method2")
time.sleep(2)
print("doing work in method2")
time.sleep(2)
当调度时间重叠时(例如0、20、30),并且调度程序为该时间调度了两个作业,它似乎会并行运行它们。输出如下所示:
doing work in method1
doing work in method2
doing work in method1
doing work in method2
doing work in method1
doing work in method2
问题是:如何设置它,以便按顺序运行挂起的作业。也就是说。如果两个作业的时间重叠,请运行第一个作业直到完成,然后运行第二个作业。
编辑:我之所以使用apsSchedule库,是因为我需要类似cron的功能。我需要以特定时间间隔在一天中的特定时间之间运行该进程。
推荐答案
使用DebugExecutor。 例如:
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.executors.debug import DebugExecutor
def foo1():
print("x")
def foo2():
time.sleep(3)
print("y")
scheduler = BlockingScheduler()
scheduler.add_executor(DebugExecutor(), "consecutive")
scheduler.add_job(foo1, 'interval', max_instances=1, seconds=1, executor="consecutive")
scheduler.add_job(foo2, 'interval', max_instances=1, seconds=5, executor="consecutive")
这篇关于使用Python APS顺序运行挂起的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文