|
24 | 24 | from . import events
|
25 | 25 | from . import exceptions
|
26 | 26 | from . import futures
|
| 27 | +from . import timeouts |
27 | 28 | from .coroutines import _is_coroutine
|
28 | 29 |
|
29 | 30 | # Helper to generate new task names
|
@@ -439,63 +440,23 @@ async def wait_for(fut, timeout):
|
439 | 440 |
|
440 | 441 | This function is a coroutine.
|
441 | 442 | """
|
442 |
| - loop = events.get_running_loop() |
443 |
| - |
444 | 443 | if timeout is None:
|
445 | 444 | return await fut
|
446 | 445 |
|
447 | 446 | if timeout <= 0:
|
448 |
| - fut = ensure_future(fut, loop=loop) |
| 447 | + fut = ensure_future(fut) |
449 | 448 |
|
450 | 449 | if fut.done():
|
451 | 450 | return fut.result()
|
452 | 451 |
|
453 |
| - await _cancel_and_wait(fut, loop=loop) |
| 452 | + await _cancel_and_wait(fut) |
454 | 453 | try:
|
455 | 454 | return fut.result()
|
456 | 455 | except exceptions.CancelledError as exc:
|
457 |
| - raise exceptions.TimeoutError() from exc |
458 |
| - |
459 |
| - waiter = loop.create_future() |
460 |
| - timeout_handle = loop.call_later(timeout, _release_waiter, waiter) |
461 |
| - cb = functools.partial(_release_waiter, waiter) |
462 |
| - |
463 |
| - fut = ensure_future(fut, loop=loop) |
464 |
| - fut.add_done_callback(cb) |
465 |
| - |
466 |
| - try: |
467 |
| - # wait until the future completes or the timeout |
468 |
| - try: |
469 |
| - await waiter |
470 |
| - except exceptions.CancelledError: |
471 |
| - if fut.done(): |
472 |
| - return fut.result() |
473 |
| - else: |
474 |
| - fut.remove_done_callback(cb) |
475 |
| - # We must ensure that the task is not running |
476 |
| - # after wait_for() returns. |
477 |
| - # See https://bugs.python.org/issue32751 |
478 |
| - await _cancel_and_wait(fut, loop=loop) |
479 |
| - raise |
480 |
| - |
481 |
| - if fut.done(): |
482 |
| - return fut.result() |
483 |
| - else: |
484 |
| - fut.remove_done_callback(cb) |
485 |
| - # We must ensure that the task is not running |
486 |
| - # after wait_for() returns. |
487 |
| - # See https://bugs.python.org/issue32751 |
488 |
| - await _cancel_and_wait(fut, loop=loop) |
489 |
| - # In case task cancellation failed with some |
490 |
| - # exception, we should re-raise it |
491 |
| - # See https://bugs.python.org/issue40607 |
492 |
| - try: |
493 |
| - return fut.result() |
494 |
| - except exceptions.CancelledError as exc: |
495 |
| - raise exceptions.TimeoutError() from exc |
496 |
| - finally: |
497 |
| - timeout_handle.cancel() |
| 456 | + raise TimeoutError from exc |
498 | 457 |
|
| 458 | + async with timeouts.timeout(timeout): |
| 459 | + return await fut |
499 | 460 |
|
500 | 461 | async def _wait(fs, timeout, return_when, loop):
|
501 | 462 | """Internal helper for wait().
|
@@ -541,9 +502,10 @@ def _on_completion(f):
|
541 | 502 | return done, pending
|
542 | 503 |
|
543 | 504 |
|
544 |
| -async def _cancel_and_wait(fut, loop): |
| 505 | +async def _cancel_and_wait(fut): |
545 | 506 | """Cancel the *fut* future or task and wait until it completes."""
|
546 | 507 |
|
| 508 | + loop = events.get_running_loop() |
547 | 509 | waiter = loop.create_future()
|
548 | 510 | cb = functools.partial(_release_waiter, waiter)
|
549 | 511 | fut.add_done_callback(cb)
|
|
0 commit comments