This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio.as_completed documentation misleading
Type: Stage: resolved
Components: asyncio, Documentation Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: aeros, aronacher, asvetlov, bar.harel, docs@python, gvanrossum, hynek, miss-islington, xtreak, yselivanov
Priority: normal Keywords: patch

Created on 2020-04-27 11:14 by bar.harel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19753 merged bar.harel, 2020-04-28 12:41
PR 20337 merged miss-islington, 2020-05-23 23:16
PR 20338 merged miss-islington, 2020-05-23 23:17
Messages (16)
msg367410 - (view) Author: Bar Harel (bar.harel) * Date: 2020-04-27 11:14
Continuing with bpo-27589, looks like as_completed documentation is still misleading. According to the docs, it "Return(s) an iterator of Future objects. Each Future object returned represents the earliest result from the set of the remaining awaitables."

There's only one problem: The only thing it definitely doesn't do, is return an iterator of future objects. To be honest with you, I'm not entirely sure how to phrase it.

For reference, I fell for this:

mapping = {fut: index for fut, index in enumerate(futures)}
for fut in as_completed(mapping):
  mapping[fut]  # KeyError
msg367430 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-04-27 15:28
I declare this not a bug.

The docs do not promise that the Futures being returned are the *same* Futures that were passed in. They are not. They are (or at least may be) new Futures that represent the same event. Since Futures, when used as dict keys, use identity as equality, those new Futures will not be present as keys in the mapping of Futures passed in by the OP.
msg367436 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-04-27 16:16
Reopening so as to give the OP one more chance to state their case. They wrote:

"""
You've immediately closed the issue so I couldn't even reply to it,
Unfortunately, it doesn't return a Future object at all, so technically you're wrong, together with the docs of course, which was the bug I reported...
"""

If it's not a Future then what? You're not showing that in your report.
msg367437 - (view) Author: Bar Harel (bar.harel) * Date: 2020-04-27 16:25
It's a coroutine. Basically the same coroutine yielded over and over, returning the first future's result each time.
Like I said, I'm not entirely sure how to phrase it.
Maybe "Returns an iterator of awaitables"?
msg367438 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-04-27 16:49
Oh, you're right. The docstring correctly says this. :-(

Do you have the power to submit a PR? I think it should just say that the return values are coroutines (which is what it does). @Yury what do you think?
msg367485 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-04-28 02:44
> @Yury what do you think?

Yeah, the documentation needs to be fixed.

> Maybe "Returns an iterator of awaitables"?

I'd suggest to change to: "Return an iterator of coroutines.  Each coroutine allows to wait for the earliest next result from the set of the remaining awaitables."
msg367621 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-04-29 03:59
Yury Selivanov wrote:
> I'd suggest to change to: "Return an iterator of coroutines.  Each coroutine allows to wait for the earliest next result from the set of the remaining awaitables."

The first sentence looks good to me, but I'm not certain about the second sentence, particularly "allows to wait". Instead, I'm thinking something like this might read better:

"Each coroutine represents the earliest next result from the set of the remaining awaitables, based upon the order of completion."

Thoughts?
msg367622 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-04-29 04:11
> based upon the order of completion

I forgot to explain this in the above message. I think something that mentions "order of completion" should be included. Although it's implied in the name `as_completed`, to me it seems worthwhile to be fully explicit with what exactly is meant by "earliest".
msg367625 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-04-29 04:20
Alternatively, I think "can wait for" would also read better than "allows to wait for", if that is preferred over my above suggestion.
msg367626 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-04-29 04:58
Please work this out in the PR between the two of you, Kyle and Bar.
msg367630 - (view) Author: Bar Harel (bar.harel) * Date: 2020-04-29 07:48
@Kyle, looks good to me. Maybe this will work better? Together with the current example which shows the "earliest_result" variable of course.

"Each coroutine returns the next result from the set of the remaining awaitables, based upon the order of completion."

Represents -> Returns (less obscure, and coroutines are awaited on to get the result)
Earliest -> removed, "order  of completion" looks great.
msg367733 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-04-30 04:01
@Bar as requested by Guido, let's continue the discussion in the PR: https://github.com/python/cpython/pull/19753#pullrequestreview-403185142.
msg369757 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-05-23 23:14
New changeset 13206b52d16c2489f4c7dd2dce2a7f48a554b5ed by Bar Harel in branch 'master':
bpo-40405: Fix asyncio.as_completed docs (GH-19753)
https://github.com/python/cpython/commit/13206b52d16c2489f4c7dd2dce2a7f48a554b5ed
msg369758 - (view) Author: miss-islington (miss-islington) Date: 2020-05-23 23:23
New changeset 9181e2e2f3ade85f65bf2521a5deb898434dfd64 by Miss Islington (bot) in branch '3.9':
bpo-40405: Fix asyncio.as_completed docs (GH-19753)
https://github.com/python/cpython/commit/9181e2e2f3ade85f65bf2521a5deb898434dfd64
msg369759 - (view) Author: miss-islington (miss-islington) Date: 2020-05-23 23:24
New changeset 2fecb48a1d84190c37214eb4b0c8d5460300a78b by Miss Islington (bot) in branch '3.8':
bpo-40405: Fix asyncio.as_completed docs (GH-19753)
https://github.com/python/cpython/commit/2fecb48a1d84190c37214eb4b0c8d5460300a78b
msg369760 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-05-23 23:25
Thanks for bring this to our attention and working on this Bar Harel, and thanks to Yury for the suggestions.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84585
2020-05-23 23:25:56aerossetstatus: open -> closed
resolution: fixed
messages: + msg369760

stage: patch review -> resolved
2020-05-23 23:24:06miss-islingtonsetmessages: + msg369759
2020-05-23 23:23:59miss-islingtonsetmessages: + msg369758
2020-05-23 23:17:07miss-islingtonsetpull_requests: + pull_request19606
2020-05-23 23:16:59miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request19605
2020-05-23 23:14:38aerossetmessages: + msg369757
2020-04-30 04:01:00aerossetmessages: + msg367733
2020-04-29 07:48:15bar.harelsetmessages: + msg367630
2020-04-29 04:58:05gvanrossumsetmessages: + msg367626
2020-04-29 04:20:47aerossetmessages: + msg367625
2020-04-29 04:11:31aerossetmessages: + msg367622
2020-04-29 03:59:48aerossetnosy: + aeros
messages: + msg367621
2020-04-28 12:41:45bar.harelsetkeywords: + patch
stage: resolved -> patch review
pull_requests: + pull_request19074
2020-04-28 02:44:56yselivanovsetmessages: + msg367485
2020-04-27 16:49:28gvanrossumsetresolution: not a bug -> (no value)
messages: + msg367438
2020-04-27 16:25:00bar.harelsetmessages: + msg367437
2020-04-27 16:16:01gvanrossumsetstatus: closed -> open

messages: + msg367436
2020-04-27 15:28:06gvanrossumsetstatus: open -> closed
resolution: not a bug
messages: + msg367430

stage: resolved
2020-04-27 13:22:02vstinnersetnosy: - vstinner
2020-04-27 11:15:01bar.harelsettitle: ast -> asyncio.as_completed documentation misleading
2020-04-27 11:14:50bar.harelcreate