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)
| 766 | |
| 767 | |
| 768 | def 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 |
searching dependent graphs…