Ajayc
Ajayc

Reputation: 843

Only run one job concurrently in apscheduler across the same scheduler

I'm trying to ensure that only one job is run at a time in the same scheduler. So for example, I may have something like this:

def echo_arg(arg):
  print 'Going to tell you the arg! The arg is: {0}'.format(arg)
  sleep(5)

def main():
  scheduler = BlockingScheduler()
  for i in range(0, 60):
      scheduler.add_job(echo_arg, 'cron', args=[i], second=i, max_instances=1)
  scheduler.start()

Even though there are 60 distinct jobs, I'd like the scheduler to block until the previous job has completed. For example, the job at 0 seconds should render the runs at 1, 2, 3, and 4 invalid. Is there some way to do this in the scheduler itself?

Thanks!

Upvotes: 2

Views: 3202

Answers (1)

Alex Grönholm
Alex Grönholm

Reputation: 5901

Yes, run them in a thread pool executor that has only 1 worker. That way no jobs can run concurrently.

scheduler = BlockingScheduler(executors={'default': ThreadPoolExecutor(1)})

If the jobs have overlapping schedules, make sure you adjust the misfire grace time from the default value.

Upvotes: 6

Related Questions