def get_signalfd(self, sig, close_on_fork=True): sigset = SIGSET() sigaddset(sigset, sig) sigprocmask(SIG_BLOCK, sigset, NULL) fd = linuxfd.signalfd(signalset={sig}, nonBlocking=True) if close_on_fork: self._close_on_fork_fds.add(fd) return fd
def run(self): global _WorkerInstance pid = os.fork() if pid != 0: self.pid = pid return _WorkerInstance = self try: closed_sock = self.socket_dispatcher.keys() for sock_id in self.listen: closed_sock.remove(sock_id) for sock_id in closed_sock: self.socket_dispatcher[sock_id] = None self.install_signal_handlers() self.close_logs() self.init_resources() self.logger(LOG_NOTICE, "Worker started") self.GO_ON = True while self.GO_ON == True: try: pysigset.sigprocmask(pysigset.SIG_UNBLOCK, _SigSetHandled, pysigset.NULL) self.loop() pysigset.sigprocmask(pysigset.SIG_BLOCK, _SigSetHandled, pysigset.NULL) except WorkerLoopInterrupt: pass except Exception as e: self.logger( LOG_ERROR, "Unexpected error while running worker loop: " + repr(e)) finally: self.handle_signals() self.release_resources() self.logger(LOG_NOTICE, "Worker stopped") except Exception as e: self.logger(LOG_ERROR, "Error running worker: " + repr(e)) finally: self.close_logs() sys.exit(0)
def release(self, fd): # TODO: singledispatch if fd not in self._close_on_fork_fds: return self._close_on_fork_fds.remove(fd) fd.close() if isinstance(fd, linuxfd.signalfd): sigset = SIGSET() for sig in fd.signals(): sigaddset(sigset, sig) logger.debug('unblock sigset %s' % sigset) sigprocmask(SIG_UNBLOCK, sigset, NULL)
def unmask_sigio(self): sigemptyset(self.sigset) sigaddset(self.sigset, signal.SIGIO) sigprocmask(SIG_UNBLOCK, self.sigset, 0)