processes.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:shelter 作者: seznam 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号