示例#1
0
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()
示例#2
0
文件: tasks.py 项目: varikmp/cpython
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()
示例#3
0
 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
示例#4
0
    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)
示例#5
0
    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)
示例#6
0
文件: pi.py 项目: VictoriaDel/odemis
    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()
示例#7
0
    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()
示例#8
0
 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.
示例#9
0
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()
示例#10
0
 def result(self, timeout=None):
     try:
         return self._greenlet.get(timeout=timeout)
     except gevent.Timeout as e:
         raise futures.TimeoutError(e)