且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

apscheduler - 多个实例

更新时间:2022-10-23 16:12:48


  1. 这取决于你如何最终使用两个调度程序实例。你是否在工作线程/进程中启动apscheduler?如果你有不止一个这样的工作人员,你将得到多个调度程序的实例。因此,您必须找到一种方法来防止调度程序多次启动,方法是在不同的进程中运行它,或者在调度程序启动时添加一些条件。


  2. 你不这样做变量是每个进程的本地。您可以做的***的事情就是建立某种远程执行系统,使用某种ReST服务或一些远程控制系统,如execnet或rpyc。


I have apscheduler running in django and it appears to work ... okay. In my project init.py, I initialize the scheduler:

scheduler = Scheduler(daemon=True)

print("\n\n\n\n\n\n\n\nstarting scheduler")

scheduler.configure({'apscheduler.jobstores.file.class': settings.APSCHEDULER['jobstores.file.class']})
scheduler.start()

atexit.register(lambda: scheduler.shutdown(wait=False))

The first problem with this is that the print shows this code is executed twice. Secondly, in other applications, I'd like to reference the scheduler, but haven't a clue how to do that. If I get another instance of a scheduler, I believe it is a separate threadpool and not the one created here.

  1. how do I get one and only one instance of apscheduler running?
  2. how do I reference that instance in other apps?

  1. That depends on how you ended up with two scheduler instances in the first place. Are you starting apscheduler in a worker thread/process? If you have more than one such worker, you're going to get multiple instances of the scheduler. So, you have to find a way to prevent the scheduler from being started more than once by either running it in a different process if possible, or adding some condition to the scheduler startup.

  2. You don't. Variables are local to each process. The best you can do is to build some kind of remote execution system, either using some kind of a ReST service or some remote control system like execnet or rpyc.