Internal helper for wait(). The fs argument must be a collection of Futures.
(fs, timeout, return_when, loop)
| 488 | return await fut |
| 489 | |
| 490 | async def _wait(fs, timeout, return_when, loop): |
| 491 | """Internal helper for wait(). |
| 492 | |
| 493 | The fs argument must be a collection of Futures. |
| 494 | """ |
| 495 | assert fs, 'Set of Futures is empty.' |
| 496 | waiter = loop.create_future() |
| 497 | timeout_handle = None |
| 498 | if timeout is not None: |
| 499 | timeout_handle = loop.call_later(timeout, _release_waiter, waiter) |
| 500 | counter = len(fs) |
| 501 | cur_task = current_task() |
| 502 | |
| 503 | def _on_completion(f): |
| 504 | nonlocal counter |
| 505 | counter -= 1 |
| 506 | if (counter <= 0 or |
| 507 | return_when == FIRST_COMPLETED or |
| 508 | return_when == FIRST_EXCEPTION and (not f.cancelled() and |
| 509 | f.exception() is not None)): |
| 510 | if timeout_handle is not None: |
| 511 | timeout_handle.cancel() |
| 512 | if not waiter.done(): |
| 513 | waiter.set_result(None) |
| 514 | futures.future_discard_from_awaited_by(f, cur_task) |
| 515 | |
| 516 | for f in fs: |
| 517 | f.add_done_callback(_on_completion) |
| 518 | futures.future_add_to_awaited_by(f, cur_task) |
| 519 | |
| 520 | try: |
| 521 | await waiter |
| 522 | finally: |
| 523 | if timeout_handle is not None: |
| 524 | timeout_handle.cancel() |
| 525 | for f in fs: |
| 526 | f.remove_done_callback(_on_completion) |
| 527 | |
| 528 | done, pending = set(), set() |
| 529 | for f in fs: |
| 530 | if f.done(): |
| 531 | done.add(f) |
| 532 | else: |
| 533 | pending.add(f) |
| 534 | return done, pending |
| 535 | |
| 536 | |
| 537 | async def _cancel_and_wait(fut): |
no test coverage detected
searching dependent graphs…