[Python-ideas] awaiting iterables
Piotr Jurkiewicz
piotr.jerzy.jurkiewicz at gmail.com
Sun May 3 00:18:05 CEST 2015
I know that. But the problem with wait() is that it returns Tasks, not
their results directly. So user has to unpack them manually.
Furthermore, after introduction of `await`, its name will become
problematic. It will reassembles `await` too much and can cause a
confusion. Its usage would result in an awkward 'await wait()'.
There is a function gather(*coros_or_futures) which returns results list
directly, like the function all_done() I proposed.
But there is no function gather_any(*coros_or_futures), to return just a
result of the first done coroutine. (One can achieve it with
wait(return_when=FIRST_COMPLETED) but as mentioned before, it does not
return a result directly, so there is no symmetry with gather())
Function as_completed() returns indeed an iterator over the futures as
they complete, but it is not compatible with the 'async for' protocol
proposed in PEP 492. So new function has to be created anyway.
Therefore I deliberately placed these functions in a new asynctools
module, not in the asyncio module: to emphasize that they are supposed
to be used with the new-style coroutines, proposed in PEP 492.
I wanted to achieve simplicity (by returning results directly) and
symmetry (all_done()/any_done()).
Piotr
On 2015-05-02 23:29, Guido van Rossum wrote:
> The asyncio package already has this functionality; check out wait() (it
> has various options), as_completed(), gather().
More information about the Python-ideas
mailing list