It's exceedingly subtle -- that's why the docstring contains an example of
how to use it.
Note the final two lines:
for _ in range(len(todo)):
yield _wait_for_one()
This is yield, not yield-from. as_completed() is not a coroutine -- it is
an iterator and the caller must loop over it. Each time the caller gets one
thing from the loop, what is that thing? It's _wait_for_one() -- i.e. it's
a coroutine! Then the caller has to use "yield from" on that coroutine,
which will then wait until a future emerges from the queue, and then
_wait_for_one() either returns that future's result or raises its
exception. This is also where timeouts are processed (they result in a
dummy None emerging from the queue).
Hope this helps. The input futures never appear in the output, so the
warning in the docstring is an understatement (but don't remove it -- a
future implementation might use an optimized path if some of the futures
are *already* done.
--Guido
On Mon, Mar 2, 2015 at 1:48 AM, Victor Stinner <[email protected]>
wrote:
> Hi,
>
> 2015-03-01 13:43 GMT+01:00 Luciano Ramalho <[email protected]>:
> > """
> > Note: The futures f are not necessarily members of fs. A given future
> > may be wrapped in another future by as_completed; when that happens,
> > the result/error of the new future will be the same as the original
> > future.
> > """
>
> I read the source code of as_completed() and I don't see where a
> future can be wrapped in another future. I only saw that async() is
> called on each item of the fs parameter, so coroutine objects are
> wrapped into future. So I propose:
>
> """
> Note: The futures f are not necessarily members of fs. Coroutine
> objects of fs are
> wrapped in futures.
> """
>
> Victor
>
--
--Guido van Rossum (python.org/~guido)