MCPcopy Index your code
hub / github.com/python/cpython / _wait

Function _wait

Lib/asyncio/tasks.py:490–534  ·  view source on GitHub ↗

Internal helper for wait(). The fs argument must be a collection of Futures.

(fs, timeout, return_when, loop)

Source from the content-addressed store, hash-verified

488 return await fut
489
490async 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
537async def _cancel_and_wait(fut):

Callers 1

waitFunction · 0.70

Calls 9

current_taskFunction · 0.85
setFunction · 0.85
create_futureMethod · 0.45
call_laterMethod · 0.45
add_done_callbackMethod · 0.45
cancelMethod · 0.45
remove_done_callbackMethod · 0.45
doneMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…