def func(): t = Timeout(0.01) t.start() try: evergreen.sleep(10) except Timeout as e: self.assertTrue(t is e)
def acquire(self, blocking=True, timeout=None): if self._counter > 0: self._counter -= 1 return True elif not blocking: return False else: current = evergreen.current.task self._waiters.add(current) timer = Timeout(timeout) timer.start() loop = evergreen.current.loop try: while self._counter <= 0: loop.switch() except Timeout as e: if e is timer: return False raise else: self._counter -= 1 return True finally: timer.cancel() self._waiters.discard(current)
def sendall(self, data, flags=0): if isinstance(data, six.text_type): data = data.encode() if self.timeout is None: data_sent = 0 while data_sent < len(data): data_sent += self.send(_get_memory(data, data_sent), flags) else: timer = Timeout(self.timeout, timeout('timed out')) timer.start() try: data_sent = 0 while True: data_sent += self.send(_get_memory(data, data_sent), flags) if data_sent >= len(data): break finally: timer.cancel()
def as_completed(fs, timeout=None): """An iterator over the given futures that yields each as it completes. Args: fs: The sequence of Futures (possibly created by different Executors) to iterate over. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait time. Returns: An iterator that yields the given Futures as they complete (finished or cancelled). Raises: TimeoutError: If the entire result iterator could not be generated before the given timeout. """ with _AcquireFutures(fs): finished = set(f for f in fs if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) pending = set(fs) - finished waiter = _create_and_install_waiters(fs, _AS_COMPLETED) timer = Timeout(timeout) timer.start() try: for future in finished: yield future while pending: waiter.event.wait() with waiter.lock: finished = waiter.finished_futures waiter.finished_futures = [] waiter.event.clear() for future in finished: yield future pending.remove(future) except Timeout as e: if timer is not e: raise raise TimeoutError('%d (of %d) futures unfinished' % (len(pending), len(fs))) finally: timer.cancel() for f in fs: f._waiters.remove(waiter)
def connect(self, address): if self.timeout == 0.0: return self._sock.connect(address) sock = self._sock if isinstance(address, tuple): r = getaddrinfo(address[0], address[1], sock.family, sock.type, sock.proto) address = r[0][-1] timer = Timeout(self.timeout, timeout('timed out')) timer.start() try: while True: err = sock.getsockopt(SOL_SOCKET, SO_ERROR) if err: raise error(err, os.strerror(err)) result = sock.connect_ex(address) if not result or result == EISCONN: break elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows): self._io.wait_write() else: raise error(result, os.strerror(result)) finally: timer.cancel()
def sleep(): with Timeout(0.01, FooTimeout): evergreen.sleep(10)
def sleep(): with Timeout(-1): evergreen.sleep(0.01)
def sleep(): with Timeout(None): evergreen.sleep(0.01)