def test_wait_channel(self): DNSResolver._channel = channel = self.mox.CreateMockAnything() poll = self.mox.CreateMockAnything() self.mox.StubOutWithMock(select, 'poll') select.poll().AndReturn(poll) channel.getsock().AndReturn(([1, 2], [2, 3])) channel.timeout().AndReturn(1.0) poll.register(1, select.POLLIN) poll.register(2, select.POLLIN | select.POLLOUT) poll.register(3, select.POLLOUT) poll.poll(1.0).AndReturn([(1, select.POLLIN), (3, select.POLLOUT)]) channel.process_fd(1, pycares.ARES_SOCKET_BAD) channel.process_fd(pycares.ARES_SOCKET_BAD, 3) channel.getsock().AndReturn(([1, 3], [4])) channel.timeout().AndReturn(1.0) poll.register(3, select.POLLIN) poll.register(4, select.POLLOUT) poll.unregister(2) poll.poll(1.0).AndReturn([]) channel.getsock().AndReturn(([1, 3], [4])) channel.timeout().AndReturn(None) channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD) channel.getsock().AndReturn(([], [])) poll.unregister(1) poll.unregister(3) poll.unregister(4) self.mox.ReplayAll() DNSResolver._wait_channel()
def test_wait_channel_error(self): DNSResolver._channel = channel = self.mox.CreateMockAnything() poll = self.mox.CreateMockAnything() self.mox.StubOutWithMock(select, 'poll') select.poll().AndReturn(poll) channel.getsock().AndReturn(([1], [])) channel.timeout().AndReturn(1.0) poll.register(1, select.POLLIN).AndReturn(None) poll.poll(1.0).AndRaise(ValueError(13)) channel.cancel() self.mox.ReplayAll() with self.assertRaises(ValueError): DNSResolver._wait_channel() self.assertIsNone(DNSResolver._channel)
def __init__(self): self.poll = select.poll() self.pipetrick = os.pipe() self.pendingWakeup = False self.runningPoll = False self.nextHandleID = 1 self.nextTimerID = 1 self.handles = [] self.timers = [] self.quit = False # The event loop can be used from multiple threads at once. # Specifically while the main thread is sleeping in poll() # waiting for events to occur, another thread may come along # and add/update/remove a file handle, or timer. When this # happens we need to interrupt the poll() sleep in the other # thread, so that it'll see the file handle / timer changes. # # Using OS level signals for this is very unreliable and # hard to implement correctly. Thus we use the real classic # "self pipe" trick. A anonymous pipe, with one end registered # with the event loop for input events. When we need to force # the main thread out of a poll() sleep, we simple write a # single byte of data to the other end of the pipe. debug("Self pipe watch %d write %d" % (self.pipetrick[0], self.pipetrick[1])) self.poll.register(self.pipetrick[0], select.POLLIN)
def _poll_socket(self, sock, events, timeout=None): # gevent.select.poll converts seconds to miliseconds to match python socket # implementation timeout = timeout * 1000 if timeout is not None else 100 poller = poll() poller.register(sock, eventmask=events) return poller.poll(timeout=timeout)
def _wait_channel(cls): poll = select.poll() fds_map = OrderedDict() try: while True: fds_map = cls._register_fds(poll, fds_map) if not fds_map: break timeout = cls._channel.timeout() if not timeout: cls._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD) continue for fd, event in poll.poll(timeout): if event & (select.POLLIN | select.POLLPRI): cls._channel.process_fd(fd, pycares.ARES_SOCKET_BAD) if event & select.POLLOUT: cls._channel.process_fd(pycares.ARES_SOCKET_BAD, fd) except Exception: logging.log_exception(__name__) cls._channel.cancel() cls._channel = None raise finally: cls._thread = None
def read(self, size=1): """\ Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read. """ if not self.is_open: raise portNotOpenError read = bytearray() timeout = Timeout(self._timeout) poll = select.poll() poll.register(self.fd.fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL) poll.register(self.pipe_abort_read_r, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL) if size > 0: while len(read) < size: # print "\tread(): size",size, "have", len(read) #debug # wait until device becomes ready to read (or something fails) for fd, event in poll.poll(None if timeout.is_infinite else (timeout.time_left() * 1000)): if fd == self.pipe_abort_read_r: break if event & (select.POLLERR | select.POLLHUP | select.POLLNVAL): raise SerialException('device reports error (poll)') # we don't care if it is select.POLLIN or timeout, that's # handled below if fd == self.pipe_abort_read_r: os.read(self.pipe_abort_read_r, 1000) break buf = os.read(self.fd.fd, size - len(read)) read.extend(buf) if timeout.expired() \ or (self._inter_byte_timeout is not None and self._inter_byte_timeout > 0) and not buf: break # early abort on timeout return bytes(read)
def is_connection_dropped(conn): # Platform-specific """ Returns True if the connection is dropped and should be closed. :param conn: :class:`httplib.HTTPConnection` object. Note: For platforms like AppEngine, this will always return ``False`` to let the platform handle connection recycling transparently for us. """ sock = getattr(conn, 'sock', False) if sock is False: # Platform-specific: AppEngine return False if sock is None: # Connection already closed (such as by httplib). return True if not poll: if not select: # Platform-specific: AppEngine return False try: return select([sock], [], [], 0.0)[0] except socket.error: return True # This version is better on platforms that support it. p = poll() p.register(sock, POLLIN) for (fno, ev) in p.poll(0.0): if fno == sock.fileno(): # Either data is buffered (bad), or the connection is dropped. return True
def test_poll_invalid(self): with open(__file__, 'rb') as fp: fd = fp.fileno() fp.close() poll = select.poll() poll.register(fd, select.POLLIN) result = poll.poll(0) self.assertEqual(result, [(fd, select.POLLNVAL)]) # pylint:disable=no-member
def wait(self, timeout): r, w = os.pipe() try: poll = select.poll() poll.register(r) poll.poll(timeout * 1000) poll.unregister(r) finally: os.close(r) os.close(w)
def _poll_socket(self, events, timeout=None): if self.sock is None: return # gevent.select.poll converts seconds to miliseconds to match python socket # implementation timeout = timeout * 1000 if timeout is not None else 100 poller = poll() poller.register(self.sock, eventmask=events) logger.debug("Polling socket with timeout %s", timeout) poller.poll(timeout=timeout)
def test_poll_invalid(self): with open(__file__, 'rb') as fp: fd = fp.fileno() poll = select.poll() poll.register(fd, select.POLLIN) # Close after registering; libuv refuses to even # create a watcher if it would get EBADF (so this turns into # a test of whether or not we successfully initted the watcher). fp.close() result = poll.poll(0) self.assertEqual(result, [(fd, select.POLLNVAL)]) # pylint:disable=no-member
def wait(self, timeout): # On darwin, the read pipe is reported as writable # immediately, for some reason. So we carefully register # it only for read events (the default is read and write) r, w = os.pipe() try: poll = select.poll() poll.register(r, select.POLLIN) poll.poll(timeout * 1000) finally: poll.unregister(r) os.close(r) os.close(w)
def test_poll_invalid(self): self.skipTest( "libev >= 4.27 aborts the process if built with EV_VERIFY >= 2. " "For libuv, depending on whether the fileno is reused or not " "this either crashes or does nothing.") with open(__file__, 'rb') as fp: fd = fp.fileno() poll = select.poll() poll.register(fd, select.POLLIN) # Close after registering; libuv refuses to even # create a watcher if it would get EBADF (so this turns into # a test of whether or not we successfully initted the watcher). fp.close() result = poll.poll(0) self.assertEqual(result, [(fd, select.POLLNVAL)]) # pylint:disable=no-member
def test_unregister_never_registered(self): # "Attempting to remove a file descriptor that was # never registered causes a KeyError exception to be # raised." poll = select.poll() self.assertRaises(KeyError, poll.unregister, 5)