Exemplo n.º 1
0
def wait_on_socket(socket, watcher, timeout_exc=None):
    if socket is None or watcher is None:
        # test__hub TestCloseSocketWhilePolling, on Python 2; Python 3
        # catches the EBADF differently.
        raise ConcurrentObjectUseError("The socket has already been closed by another greenlet")
    _primitive_wait(watcher, socket.timeout,
                    timeout_exc if timeout_exc is not None else _NONE,
                    socket.hub)
Exemplo n.º 2
0
 def get(self):
     """If a value/an exception is stored, return/raise it. Otherwise until switch() or throw() is called."""
     if self._exception is not _NONE:
         if self._exception is None:
             return self.value
         getcurrent().throw(*self._exception) # pylint:disable=undefined-variable
     else:
         if self.greenlet is not None:
             raise ConcurrentObjectUseError('This Waiter is already used by %r' % (self.greenlet, ))
         self.greenlet = getcurrent() # pylint:disable=undefined-variable
         try:
             return self.hub.switch()
         finally:
             self.greenlet = None
def _primitive_wait(watcher, timeout, timeout_exc, hub):
    if watcher.callback is not None:
        raise ConcurrentObjectUseError(
            'This socket is already used by another greenlet: %r' %
            (watcher.callback, ))

    if hub is None:
        hub = get_hub()

    if timeout is None:
        hub.wait(watcher)
        return

    timeout = Timeout._start_new_or_dummy(
        timeout, (timeout_exc if timeout_exc is not _NONE or timeout is None
                  else _timeout_error('timed out')))

    with timeout:
        hub.wait(watcher)