Pregunta

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?
¿Fue útil?

Solución

  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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top