def register(): ''' This function creates a select.poll object that can be used in the same manner as signal.pause(). The poll object returns each time a signal was received by the process. This function has to be called from the main thread. ''' global _signal_poller global _signal_read_fd if _signal_poller is not None: raise RuntimeError('register was already called') read_fd, write_fd = os.pipe() # Python c-level signal handler requires that the write end will be in # non blocking mode filecontrol.set_non_blocking(write_fd) # Set the read pipe end to non-blocking too, just in case. filecontrol.set_non_blocking(read_fd) # Prevent subproccesses we execute from inheriting the pipes. filecontrol.set_close_on_exec(write_fd) filecontrol.set_close_on_exec(read_fd) signal.set_wakeup_fd(write_fd) poller = select.poll() poller.register(read_fd, select.POLLIN) _signal_poller = poller _signal_read_fd = read_fd
def __init__(self, map): rfd, wfd = os.pipe() asyncore.file_dispatcher.__init__(self, rfd, map=map) os.close(rfd) # file_dispatcher duped it filecontrol.set_close_on_exec(self._fileno) filecontrol.set_close_on_exec(wfd) filecontrol.set_non_blocking(wfd) self._wfd = wfd
def test_blocking(self): with open_fd() as fd: self.assertEqual(0, filecontrol.set_non_blocking(fd, False)) self.assertFalse(os.O_NONBLOCK & fcntl.fcntl(fd, fcntl.F_GETFL))