[issue27589] asyncio doc: issue in as_completed() doc
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from STINNER Victor: Remark on as_completed() doc by Hynek. https://docs.python.org/dev/library/asyncio-task.html#asyncio.as_completed Futures are yielded in an unexpected order: as soon as a future completes. ---------- assignee: docs@python components: Documentation, asyncio messages: 270982 nosy: docs@python, gvanrossum, haypo, yselivanov priority: normal severity: normal status: open title: asyncio doc: issue in as_completed() doc versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Hynek Schlawack added the comment: More explicitly: The doc sells the function short. If you have a bunch of futures and want to know as soon as one of them is ready: this is the function for you. The only hint that this is the actual behavior comes from the *name* of the function; not the documentation. ---------- nosy: +hynek _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Armin Ronacher added the comment: I am not even sure what the function is supposed to tell me. The documentation is very unclear and the example code does not help. What is "fs" for instance? And why would it return things that are not from fs? ---------- nosy: +aronacher _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Guido van Rossum added the comment: However, in general you should use a different pattern. Wrap each future in a coroutine that does whatever you want to do to the first one ready and maybe cancel the others. Then gather() all coroutines with the flag that keeps errors. --Guido (mobile) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Hynek Schlawack added the comment: Such an idiom is IMHO not the main usefulness of this function tho. As an (untested) example, something like async def f(n): await asyncio.sleep(n) return n for f in asyncio.as_completed([f(3), f(2), f(1)]): print(await f) will print: 1 2 3 That’s *super* useful if you’re coordinating multiple independent external systems and need to process their results as soon as they arrive (and not once they’re *all* done). Maybe it always worked by accident for me but it’s my understanding, that that is what this function is for (and I haven’t found another way to achieve it). That’s why it would be nice if there’d be authoritative docs on what it’s supposed to do. :) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Guido van Rossum added the comment: Well, in that case the idiom will be even simpler: wrap each one in a coroutine that awaits it and prints the result and then gather() all the coroutine wrappers. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment: Current docs read as below with an example to show that earliest future is returned. I guess this can be closed. https://docs.python.org/dev/library/asyncio-task.html#asyncio.as_completed Run awaitable objects in the aws set concurrently. Return an iterator of Future objects. Each Future object returned represents the earliest result from the set of the remaining awaitables. ---------- nosy: +asvetlov, xtreak _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27589> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by Andrew Svetlov <andrew.svetlov@gmail.com>: ---------- resolution: -> fixed stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue27589> _______________________________________
participants (6)
-
Andrew Svetlov
-
Armin Ronacher
-
Guido van Rossum
-
Hynek Schlawack
-
Karthikeyan Singaravelan
-
STINNER Victor