asyncio.gather
and asyncio.wait
seem to have similar uses: I have a bunch of async things that I want to execute/wait for (not necessarily waiting for one to finish before the next one starts). They use a different syntax, and differ in some details, but it seems very un-pythonic to me to have 2 functions that have such a huge overlap in functionality. What am I missing?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
asyncio.wait
is more low level thanasyncio.gather
.As the name suggests,
asyncio.gather
mainly focuses on gathering the results. it waits on a bunch of futures and return their results in a given order.asyncio.wait
just waits on the futures. and instead of giving you the results directly, it gives done and pending tasks. you have to mannually collect the values.Moreover, you could specify to wait for all futures to finish or the just the first one with
wait
.Although similar in general cases ("run and get results for many tasks"), each function has some specific functionality for other cases:
asyncio.gather()
Returns a Future instance, allowing high level grouping of tasks:
All tasks in a group can be cancelled by calling
group2.cancel()
or evenall_groups.cancel()
. See also.gather(..., return_exceptions=True)
,asyncio.wait()
Supports waiting to be stopped after the first task is done, or after a specified timeout, allowing lower level precision of operations:
I also noticed that you can provide a group of coroutines in wait() by simply specifying the list:
Whereas grouping in gather() is done by just specifying multiple coroutines: