def run(self):
"""
Repeatedly call :meth:`loop` method every :attribute:`interval`
seconds. In case of *separate_process* is :const:`True` exit
when parent process has exited.
"""
if self.separate_process:
setproctitle.setproctitle(self.name)
self.context.config.configure_logging()
# Register SIGINT handler which will exit service process
def sigint_handler(dummy_signum, dummy_frame):
"""
Exit service process when SIGINT is reached.
"""
self.stop()
signal.signal(signal.SIGINT, sigint_handler)
next_loop_time = 0
while 1:
# Exit if service process is run in separated process and pid
# of the parent process has changed (parent process has exited
# and init is new parent) or if stop flag is set.
if (
(self.separate_process and os.getppid() != self._parent_pid) or
self._stop_event.is_set()
):
break
# Repeatedly call loop method. After first call set ready flag.
if time.time() >= next_loop_time:
self.loop()
if not next_loop_time and not self.ready:
self._lock.acquire()
try:
self._ready.value = True
finally:
self._lock.release()
next_loop_time = time.time() + self.interval
else:
time.sleep(0.1)
评论列表
文章目录