仅在同一调度程序的 apscheduler 中同时运行一项作业

Aja*_*ayc 2 python cron daemon apscheduler

我试图确保同一调度程序中一次只运行一项作业。例如,我可能有这样的事情:

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()
Run Code Online (Sandbox Code Playgroud)

尽管有 60 个不同的作业,但我希望调度程序能够阻塞,直到前一个作业完成。例如,第 0 秒的作业应使第 1、2、3 和 4 秒的运行无效。有没有办法在调度程序本身中做到这一点?

谢谢!

Ale*_*olm 6

是的,在只有 1 个工作线程的线程池执行器中运行它们。这样就没有作业可以同时运行。

scheduler = BlockingScheduler(executors={'default': ThreadPoolExecutor(1)})
Run Code Online (Sandbox Code Playgroud)

如果作业有重叠的计划,请确保调整默认值的失火宽限时间。