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

Function gather

Lib/asyncio/tasks.py:768–908  ·  view source on GitHub ↗

Return a future aggregating results from the given coroutines/futures. Coroutines will be wrapped in a future and scheduled in the event loop. They will not necessarily be scheduled in the same order as passed in. All futures must share the same event loop. If all the tasks are

(*coros_or_futures, return_exceptions=False)

Source from the content-addressed store, hash-verified

766
767
768def gather(*coros_or_futures, return_exceptions=False):
769 """Return a future aggregating results from the given coroutines/futures.
770
771 Coroutines will be wrapped in a future and scheduled in the event
772 loop. They will not necessarily be scheduled in the same order as
773 passed in.
774
775 All futures must share the same event loop. If all the tasks are
776 done successfully, the returned future's result is the list of
777 results (in the order of the original sequence, not necessarily
778 the order of results arrival). If *return_exceptions* is True,
779 exceptions in the tasks are treated the same as successful
780 results, and gathered in the result list; otherwise, the first
781 raised exception will be immediately propagated to the returned
782 future.
783
784 Cancellation: if the outer Future is cancelled, all children (that
785 have not completed yet) are also cancelled. If any child is
786 cancelled, this is treated as if it raised CancelledError --
787 the outer Future is *not* cancelled in this case. (This is to
788 prevent the cancellation of one child to cause other children to
789 be cancelled.)
790
791 If *return_exceptions* is False, cancelling gather() after it
792 has been marked done won't cancel any submitted awaitables.
793 For instance, gather can be marked done after propagating an
794 exception to the caller, therefore, calling ``gather.cancel()``
795 after catching an exception (raised by one of the awaitables) from
796 gather won't cancel any other awaitables.
797 """
798 if not coros_or_futures:
799 loop = events.get_event_loop()
800 outer = loop.create_future()
801 outer.set_result([])
802 return outer
803
804 loop = events._get_running_loop()
805 if loop is not None:
806 cur_task = current_task(loop)
807 else:
808 cur_task = None
809
810 def _done_callback(fut, cur_task=cur_task):
811 nonlocal nfinished
812 nfinished += 1
813
814 if cur_task is not None:
815 futures.future_discard_from_awaited_by(fut, cur_task)
816
817 if outer is None or outer.done():
818 if not fut.cancelled():
819 # Mark exception retrieved.
820 fut.exception()
821 return
822
823 if not return_exceptions:
824 if fut.cancelled():
825 # Check if 'fut' is cancelled first, as

Calls 11

current_taskFunction · 0.85
ensure_futureFunction · 0.85
_GatheringFutureClass · 0.85
_done_callbackFunction · 0.85
_get_loopMethod · 0.80
get_event_loopMethod · 0.45
create_futureMethod · 0.45
set_resultMethod · 0.45
doneMethod · 0.45
appendMethod · 0.45
add_done_callbackMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…