2018-09-17 00:27:00 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from threading import Event
|
|
|
|
|
|
|
|
from apscheduler.schedulers.base import BaseScheduler, STATE_STOPPED
|
|
|
|
from apscheduler.util import TIMEOUT_MAX
|
|
|
|
|
|
|
|
|
|
|
|
class BlockingScheduler(BaseScheduler):
|
|
|
|
"""
|
|
|
|
A scheduler that runs in the foreground
|
|
|
|
(:meth:`~apscheduler.schedulers.base.BaseScheduler.start` will block).
|
|
|
|
"""
|
|
|
|
_event = None
|
|
|
|
|
|
|
|
def start(self, *args, **kwargs):
|
2021-10-21 22:36:45 +00:00
|
|
|
if self._event is None or self._event.is_set():
|
|
|
|
self._event = Event()
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
super(BlockingScheduler, self).start(*args, **kwargs)
|
|
|
|
self._main_loop()
|
|
|
|
|
|
|
|
def shutdown(self, wait=True):
|
|
|
|
super(BlockingScheduler, self).shutdown(wait)
|
|
|
|
self._event.set()
|
|
|
|
|
|
|
|
def _main_loop(self):
|
|
|
|
wait_seconds = TIMEOUT_MAX
|
|
|
|
while self.state != STATE_STOPPED:
|
|
|
|
self._event.wait(wait_seconds)
|
|
|
|
self._event.clear()
|
|
|
|
wait_seconds = self._process_jobs()
|
|
|
|
|
|
|
|
def wakeup(self):
|
|
|
|
self._event.set()
|