apscheduler - multiple instances

纵饮孤独 提交于 2019-12-09 22:59:50

问题


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:


  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.



来源:https://stackoverflow.com/questions/23507921/apscheduler-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!