apscheduler - 多个实例 [英] apscheduler - multiple instances

查看:610
本文介绍了apscheduler - 多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有apscheduler运行在django,它似乎工作...好的。在我的项目 init .py中,我初始化调度程序:

  scheduler = Scheduler(daemon = True )

print(\\\
\\\
\\\
\\\
\\\
\\\
\\\
\\\
n启动调度程序)

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

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

第一个问题是打印显示此代码是执行两次。其次,在其他应用程序中,我想引用调度程序,但是不知道如何做到这一点。如果我得到另一个调度程序的实例,我相信它是一个单独的线程池,而不是在这里创建的。


  1. 我如何获得一个只有一个apscheduler的一个实例运行?

  2. 如何在其他应用程序中引用该实例?


解决方案


  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.

这篇关于apscheduler - 多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆