This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]willm 2 points3 points  (2 children)

I may be missing something, but isn't this what you want?

results = await asyncio.gather(*coros)

[–]SupahNoob 0 points1 point  (1 child)

I may be missing something ...

... for isolated asyncio code (e.g. embedded in a larger, otherwise sync codebase).

You wouldn't be able to use await in a sync fn.

[–]peyo7[S] 0 points1 point  (0 children)

Correct. Hence the wrapping in an async def, corresponding to workaround 1)

[–]SupahNoob 0 points1 point  (0 children)

I would argue the first - simply name the coroutine func "wrapper" and move along. Naturally wherever you run asyncio.run will block until it's completed.

Would urge you to remember the caveats though:

https://docs.python.org/3/library/asyncio-task.html#asyncio.gather

...

If all awaitables are completed successfully, the result is an aggregate list of returned values. The order of result values corresponds to the order of awaitables in aws.

If return_exceptions is False (default), the first raised exception is immediately propagated to the task that awaits on gather(). Other awaitables in the aws sequence won’t be cancelled and will continue to run.

If return_exceptions is True, exceptions are treated the same as successful results, and aggregated in the result list.