-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Improve docs for await expression #83266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For context, I decided to open this issue after receiving a substantial volume of very similar questions and misconceptions from users of asyncio and trio about what Also, speaking from personal experience, I did not have a clear understanding of what As a result, I think the documentation for the
(https://docs.python.org/3/reference/expressions.html#await-expression) (Also, note that there's a typo in the current version, "of coroutine" should probably be "of a coroutine") While this explanation is technically accurate, it also looks to be the _shortest_ one out of all of the defined expressions on the page. To me, this doesn't seem right considering that the await expression is not the easiest one to learn or understand. The vast majority of the questions and misunderstandings on
From what I can tell, (1) is unclear to them is partly because the awaitable object that is after the I think this would be much more clear if it were to instead be something along the lines of one the following (changes indicated with *):
Where "the current coroutine function" is the coroutine function that contains the await expression. I think this would help to clear up the first question, "What exactly is being suspended?".
This would likely help to clear up "When is it resumed/unsuspended?". Optimally, this definition could also include some form of example code like several of the other expressions have. It's not particularly easy to use a demonstrable example without using an async library (such as asyncio), but using a specific async library would not make sense to have in this location of the docs because the language reference is supposed to be as implementation agnostic as possible. However, I think a very brief visual example with some explanation could still be useful for explaining the basics of how await works:
When the coroutine function Suspension can be used to indicate that other coroutines can be executed in the meantime. This can be used to write asynchronous and concurrent programs without the usage of callbacks. Including the brief example and explanation would likely help to further clear up all three of the questions. The present version has a high degree of technical accuracy, but I don't think its as helpful as it could be for furthering the understanding of users or providing an introduction to the await expression. I'm sure that there will still be some questions regarding I'm entirely open to any alternative suggestions, or making a change that's some combination or variation of the above three ideas. Alternatively, if there are determined to be no suitable changes that would be both technically accurate and more helpful to users, I could just apply a fix to the typo. If any of these ideas are approved, I'll likely open a PR. |
Thanks for raising the very interesting question! Sorry, my English is bad; I cannot help with docs too much. Anyway, technically an awaited coroutine *can* be suspended but the suspension is not always necessary. The most deep awaited function decides. For example, if you want to read 16 bytes from a stream and these bytes are already fetched there is no suspension at this point (at least libraries are designed in this way usually). Also, technical speaking about awaits is hard without telling that a coroutine is a specialized version of generator object with (partially) overlapped methods and properties, e.g. send() and throw(). To run a coroutine you need a framework which calls these methods depending on the framework rules, the rules for asyncio are different from trio. Not sure how long should be the section but looking on |
No problem. Your feedback is still incredibly helpful and very much appreciated either way. (:
Ah, I see. It took a bit of experimentation for me to understand how this works, but I think that I get it now. Specifically, the suspension occurs when the deepest coroutine function awaits an awaitable object and a
Output:
After realizing that the suspend only occurs when In sum, a Is my interpretation mostly correct? I want to make sure that I have a good understanding of how await really works, as that will both help with improving the documentation of the await expression and improve my understanding of asyncio.
Good point. If I understand correctly, send() and throw() were specifically added to the generator API in PEP-342 for the purpose of implementing coroutines in the first place, so it makes sense to explain how they relate to await.
That's mainly why I added Nathaniel to the nosy list. I wanted to make sure that we describe the await expression in a way that's as accurate and informative as possible for both, as well as any other async library that uses await.
That would be a great goal to move towards, but I think that might have to be completed in multiple steps over a longer period of time rather than in a single change. Even if it ends up being not quite as long as 2-3 times the length of the reference for the yield expression, I think we can still make a substantial improvement to the existing version. |
There are a few other places on the documentation that are imprecise or misleading for await. While the information needed is scattered around the docs, I think these can also be improved. I'm pretty sure these fit with this issue. Developing with asyncio guide: The same sentence is found in the Task documentation: https://docs.python.org/3/library/asyncio-task.html#task-object |
Please keep me posted on any PR for this. This is a very tricky issue, since what the language promises about It is just a fact that the language reference is generally not the place people should go to learn first about language features. But clearly the one sentence about |
Related to #79012 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: