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(). I
(fut, timeout)
| 434 | |
| 435 | |
| 436 | async def wait_for(fut, timeout): |
| 437 | """Wait for the single Future or coroutine to complete, with timeout. |
| 438 | |
| 439 | Coroutine will be wrapped in Task. |
| 440 | |
| 441 | Returns result of the Future or coroutine. When a timeout occurs, |
| 442 | it cancels the task and raises TimeoutError. To avoid the task |
| 443 | cancellation, wrap it in shield(). |
| 444 | |
| 445 | If the wait is cancelled, the task is also cancelled. |
| 446 | |
| 447 | This function is a coroutine. |
| 448 | """ |
| 449 | loop = events.get_running_loop() |
| 450 | |
| 451 | if timeout is None: |
| 452 | return await fut |
| 453 | |
| 454 | if timeout <= 0: |
| 455 | fut = ensure_future(fut, loop=loop) |
| 456 | |
| 457 | if fut.done(): |
| 458 | return fut.result() |
| 459 | |
| 460 | await _cancel_and_wait(fut, loop=loop) |
| 461 | try: |
| 462 | return fut.result() |
| 463 | except exceptions.CancelledError as exc: |
| 464 | raise exceptions.TimeoutError() from exc |
| 465 | |
| 466 | waiter = loop.create_future() |
| 467 | timeout_handle = loop.call_later(timeout, _release_waiter, waiter) |
| 468 | cb = functools.partial(_release_waiter, waiter) |
| 469 | |
| 470 | fut = ensure_future(fut, loop=loop) |
| 471 | fut.add_done_callback(cb) |
| 472 | |
| 473 | try: |
| 474 | # wait until the future completes or the timeout |
| 475 | try: |
| 476 | await waiter |
| 477 | except exceptions.CancelledError: |
| 478 | if fut.done(): |
| 479 | return fut.result() |
| 480 | else: |
| 481 | fut.remove_done_callback(cb) |
| 482 | # We must ensure that the task is not running |
| 483 | # after wait_for() returns. |
| 484 | # See https://bugs.python.org/issue32751 |
| 485 | await _cancel_and_wait(fut, loop=loop) |
| 486 | raise |
| 487 | |
| 488 | if fut.done(): |
| 489 | return fut.result() |
| 490 | else: |
| 491 | fut.remove_done_callback(cb) |
| 492 | # We must ensure that the task is not running |
| 493 | # after wait_for() returns. |
nothing calls this directly
no test coverage detected