async def wait_for(fut, timeout, *, loop=None): """Wait for the single Future or coroutine to complete, with timeout. Coroutine will be wrapped in Task. Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises TimeoutError. To avoid the task cancellation, wrap it in shield(). If the wait is cancelled, the task is also cancelled. This function is a coroutine. """ if loop is None: loop = events.get_event_loop() if timeout is None: return await fut if timeout <= 0: fut = ensure_future(fut, loop=loop) if fut.done(): return fut.result() fut.cancel() raise futures.TimeoutError() waiter = loop.create_future() timeout_handle = loop.call_later(timeout, _release_waiter, waiter) cb = functools.partial(_release_waiter, waiter) fut = ensure_future(fut, loop=loop) fut.add_done_callback(cb) try: # wait until the future completes or the timeout try: await waiter except futures.CancelledError: fut.remove_done_callback(cb) fut.cancel() raise if fut.done(): return fut.result() else: fut.remove_done_callback(cb) # We must ensure that the task is not running # after wait_for() returns. # See https://bugs.python.org/issue32751 await _cancel_and_wait(fut, loop=loop) raise futures.TimeoutError() finally: timeout_handle.cancel()
def wait_for(fut, timeout, *, loop=None): """Wait for the single Future or coroutine to complete, with timeout. Coroutine will be wrapped in Task. Returns result of the Future or coroutine. Raises TimeoutError when timeout occurs. Usage: result = yield from asyncio.wait_for(fut, 10.0) """ if loop is None: loop = events.get_event_loop() waiter = futures.Future(loop=loop) timeout_handle = loop.call_later(timeout, _release_waiter, waiter, False) cb = functools.partial(_release_waiter, waiter, True) fut = async (fut, loop=loop) fut.add_done_callback(cb) try: if (yield from waiter): return fut.result() else: fut.remove_done_callback(cb) raise futures.TimeoutError() finally: timeout_handle.cancel()
def poll_server(): response = client.result(pb2.InteropResultRequest(id=run_id)) while response.status.status == pb2.RUNNING: logger.debug("Waiting for test run %s to finish...", run_id) if timeout_event.wait(POLL_INTERVAL): raise futures.TimeoutError() response = client.result(pb2.InteropResultRequest(id=run_id)) return response
def exception(self, timeout=None): if self.cancelled(): raise futures.CancelledError self._done_event.wait(timeout) if self.done(): return self._exception else: raise futures.TimeoutError(timeout)
def result(self, timeout=None): if self.cancelled(): raise futures.CancelledError # wait for task to be done event if not done already self._done_event.wait(timeout) if self.done(): if self._exception is not None: raise self._exception else: return self._result else: # Raise exception if task not done by timeout raise futures.TimeoutError(timeout)
def result(self, timeout=None): with self._condition: if self._state == CANCELLED: raise futures.CancelledError() elif self._state == FINISHED: return None self._condition.wait(timeout) if self._state == CANCELLED: raise futures.CancelledError() elif self._state == FINISHED: return None else: raise futures.TimeoutError()
def exception(self, timeout=None): with self._condition: if self._state == CANCELLED: raise cfutures.CancelledError() elif self._state == FINISHED: return self._exception self._condition.wait(timeout) if self._state == CANCELLED: raise cfutures.CancelledError() elif self._state == FINISHED: return self._exception else: raise cfutures.TimeoutError()
def _wait_for_one(): while not completed: timeout = None if deadline is not None: timeout = deadline - loop.time() if timeout < 0: raise futures.TimeoutError() done, pending = yield from _wait( todo, timeout, FIRST_COMPLETED, loop) # Multiple callers might be waiting for the same events # and getting the same outcome. Dedupe by updating todo. for f in done: if f in todo: todo.remove(f) completed.append(f) f = completed.popleft() return f.result() # May raise.
def wait_for(fut, timeout, *, loop=None): """Wait for the single Future or coroutine to complete, with timeout. Coroutine will be wrapped in Task. Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises TimeoutError. To avoid the task cancellation, wrap it in shield(). If the wait is cancelled, the task is also cancelled. This function is a coroutine. """ if loop is None: loop = events.get_event_loop() if timeout is None: return (yield from fut) waiter = futures.Future(loop=loop) timeout_handle = loop.call_later(timeout, _release_waiter, waiter) cb = functools.partial(_release_waiter, waiter) fut = ensure_future(fut, loop=loop) fut.add_done_callback(cb) try: # wait until the future completes or the timeout try: yield from waiter except futures.CancelledError: fut.remove_done_callback(cb) fut.cancel() raise if fut.done(): return fut.result() else: fut.remove_done_callback(cb) fut.cancel() raise futures.TimeoutError() finally: timeout_handle.cancel()
def result(self, timeout=None): try: return self._greenlet.get(timeout=timeout) except gevent.Timeout as e: raise futures.TimeoutError(e)