Shorthand for waiting for all the coroutines in the iterable given in parallel. Creates a task for each coroutine. Returns a list of results in the original order. If any single task raised an exception, this is raised. If multiple tasks raised exceptions, an AsyncException is raised con
(
iterable: Iterable[Coroutine], timeout: int = GENERAL_TIMEOUT
)
| 60 | |
| 61 | |
| 62 | async def wait_all( |
| 63 | iterable: Iterable[Coroutine], timeout: int = GENERAL_TIMEOUT |
| 64 | ) -> list: |
| 65 | """Shorthand for waiting for all the coroutines in the iterable given in parallel. Creates |
| 66 | a task for each coroutine. |
| 67 | Returns a list of results in the original order. If any single task raised an exception, this is raised. |
| 68 | If multiple tasks raised exceptions, an AsyncException is raised containing all exceptions. |
| 69 | """ |
| 70 | tasks = [asyncio.create_task(c) for c in iterable] |
| 71 | if not tasks: |
| 72 | return [] |
| 73 | _, pending = await asyncio.wait(tasks, timeout=timeout) |
| 74 | if pending: |
| 75 | for task in pending: |
| 76 | task.cancel() |
| 77 | raise asyncio.TimeoutError() |
| 78 | results = [] |
| 79 | errors = [] |
| 80 | for task in tasks: |
| 81 | try: |
| 82 | results.append(task.result()) |
| 83 | except Exception as e: |
| 84 | errors.append(e) |
| 85 | if errors: |
| 86 | if len(errors) == 1: |
| 87 | raise errors[0] |
| 88 | raise AsyncException(errors) |
| 89 | return [task.result() for task in tasks] |
| 90 | |
| 91 | |
| 92 | class AsyncException(Exception): |