Reputation: 621
Suppose I have two cron triggers:
trigger1 = CronTrigger(second='0,20,40')
trigger2 = CronTrigger(second='0,10,20,30,40,50')
and I create my scheduler like this:
scheduler = BlockingScheduler()
scheduler.add_job(lambda: method1(param1, param2), trigger=trigger1)
scheduler.add_job(lambda: method2(param1, param3), trigger=trigger2)
with these two methods which do work:
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)
When the scheduled times overlap(eg 0, 20, 30) and the scheduler has two jobs scheduled for that time, it seems to run them in parellel. The output looks like this:
doing work in method1
doing work in method2
doing work in method1
doing work in method2
doing work in method1
doing work in method2
Question is: How do I set it up so that the pending jobs are run sequentially. ie. if the times of two jobs overlap, run the first job until completion, then run the second one.
Edit: The reason I have used the apsschedule library is because I need cron-like functionality. I need the process to run between certain times of the day at certain intervals.
Upvotes: 1
Views: 1036
Reputation: 717
Use DebugExecutor
is a good idea, additionally I needed to specify a high value for the misfire_grace_time parameter in .add_job()
to avoid skipping runs when multiple jobs have same execution interval
Upvotes: 0
Reputation: 2712
Use DebugExecutor. For example:
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")
Upvotes: 1