On Tue, Mar 3, 2015 at 1:49 AM, Guido van Rossum <[email protected]> wrote: > 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).
Thank you for this, Guido! I can now appreciate the subtle wording of "Return an iterator whose values, when waited for, are Future instances." Is there a name for these _wait_for_one generators that yield a Future? Giving those objects a good name could make it easier to explain. As it stands, the note is misleading: "The futures f are not necessarily members of fs." The f you get from as_completed is not a Future, but that thing-you-yield-from-to-get-a-Future. Without giving that thing a name, the note could say "The future yielded from each f is not necessarily a member of fs." > 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. Do you mean it's possible some day as_completed() may return either things-you-yield-from-to-get-a-Future or actual Futures, i.e. instances of asyncio.Future? Best, Luciano > > --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) -- Luciano Ramalho Twitter: @ramalhoorg Professor em: http://python.pro.br Twitter: @pythonprobr
