Skip to content

Conversation

anordin95
Copy link
Contributor

@anordin95 anordin95 commented Aug 22, 2025

:mod:`!asyncio` automatically associates tasks with the event loop for you.
Typically there's only one event loop, so that's quite straightforward.
It's uncommon, but some applications use multithreading and :mod:`!asyncio`
together, where there's one event loop per thread, stored in thread-local
Copy link
Contributor

@kumaraditya303 kumaraditya303 Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"stored in thread-local" is unnecesary implemenetation detail for the reader, just per thread seems sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion here: #138073 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also be OK with just "per thread". But, I don't totally understand the rationale to clarify thread-locality here -- what was wrong with the original wording?

Copy link
Contributor Author

@anordin95 anordin95 Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph states asyncio automatically associates tasks with the event loop so you, the user, don't need to specify the loop in the asyncio.create_task constructor (nor are you even allowed to). The "per thread" part implies there can be multiple event loops (via multithreading) in the same memory space.

I think it's quite natural to then wonder how tasks will be associated with event loops when there are multiple event loops to choose from. And how you, the user, could manage which loop a task is assigned to. I see that as an easy way for the reader to get confused. The point of this article is to be explanatory.

While, yes, it is an implementation detail, it's not some arcane, fragile aspect. It's a fundamental design pattern exposed by the OS. And, it appears this usage of thread-local hasn't changed in the 12 years that asyncio has been around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread locality is an implementation detail and should stay as an implementation detail, -1 for adding this.

Copy link
Contributor Author

@anordin95 anordin95 Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is largely what you wrote the first time around. The logical gap I highlighted would still exist and potentially confuse readers. I agree, implementation details should generally be kept in the code, but not always. Sparsely and judiciously exposing broad ideas can ultimately create more explanatory and helpful docs, as opposed to making many vague, abstract statements, especially since this is a HOWTO, not reference, doc. Could you clarify some of the reasoning behind why you feel this way?

Like I mentioned in the prior PR, perhaps you can see how it's difficult to productively engage with this style of feedback and discussion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alex, I sympathize with thread-locality being quite confusing, but I'm going to side with Kumar here. asyncio really isn't about multithreading at all, and as you said, this is an explanatory HOWTO article. We should try to avoid bringing up niche use-cases that make the reader think "oh, asyncio and multithreading are friends, let's do that more".

As a compromise for now, would you mind reverting these changes and putting them in a follow-up PR so we can argue about it there? The rest of the changes in this PR look good to me, and it would better for those to not be blocked by discussions about this.

(We really do value your article as a contribution, please don't take this the wrong way!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhm fair enough :). Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #138200

@@ -251,6 +253,10 @@ different ways::
In a crucial way, the behavior of ``await`` depends on the type of object
being awaited.

^^^^^^^^^^
await task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await task
Awaiting Tasks

Copy link
Contributor Author

@anordin95 anordin95 Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the original for a few reasons.

The titles come from the references made in the broader await section intro.

They offer a nudge to think about await beyond just the keyword itself and emphasize how the object await applies to is fundamentally important to the operation.

And they fit more naturally in the broader table of contents:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await task
Awaiting tasks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hugo's suggestion would be my preference here and below.

@@ -282,6 +288,10 @@ This is a basic, yet reliable mental model.
In practice, the control handoffs are slightly more complex, but not by much.
In part 2, we'll walk through the details that make this possible.

^^^^^^^^^^^^^^^
await coroutine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await coroutine
Awaiting Coroutines

Copy link
Member

@hugovk hugovk Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use sentence case for headers:

https://devguide.python.org/documentation/style-guide/#capitalization

Suggested change
await coroutine
Awaiting coroutines

(We could also fix "A Conceptual Overview of asyncio" -> "A conceptual overview of asyncio" and "Event Loop" -> "Event loop".)

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I fixed the other sentence-case examples you mentioned. But, I left await task and await coroutine for the reasons described here: #138073 (comment)

:mod:`!asyncio` automatically associates tasks with the event loop for you.
Typically there's only one event loop, so that's quite straightforward.
It's uncommon, but some applications use multithreading and :mod:`!asyncio`
together, where there's one event loop per thread, stored in thread-local
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread locality is an implementation detail and should stay as an implementation detail, -1 for adding this.

@bedevere-app
Copy link

bedevere-app bot commented Aug 26, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Hugo's suggested title changes, I am fine with the current state of this document.

Let's strive to keep this conceptual overview fairly high-level. The doc's purpose should stay focused on an overview and avoid going too far into a technical deep dive.

@anordin95
Copy link
Contributor Author

anordin95 commented Sep 3, 2025

I think all requested changes have been addressed in this PR, besides modifications to the await task and await coroutine section titles requested by @hugovk & @willingc.

For ease of reference, the style guide says: "In the Python documentation, the use of sentence case in section titles is preferable, but consistency within a unit is more important than following this rule.".
I think it's fair to say these titles are consistent with the broader unit title: await.

Additionally, I think the use of "preferable" above implies there can be reasonable cases that ignore this guidance. I personally prefer the titles as they are now, for the reasons enumerated here.

With that in mind, I wonder if leaving them as is would be amenable for folks?

@hugovk
Copy link
Member

hugovk commented Sep 4, 2025

Please do rename them to "Awaiting tasks" and "Awaiting coroutines".

For ease of reference, the style guide says: "In the Python documentation, the use of sentence case in section titles is preferable, but consistency within a unit is more important than following this rule.".
I think it's fair to say these titles are consistent with the broader unit title: await.

That's referring to the await keyword, so is lowercase. Should it be in code formatting, and/or reworded?

The next sentence of the devguide:

If you add a section to a chapter where most sections are in title case, you can either convert all titles to sentence case or use the dominant style in the new section title.

image

Well, the rest is already in sentence case, let's stick to it.

@anordin95
Copy link
Contributor Author

anordin95 commented Sep 4, 2025

For reference, I interpreted the style guide as providing some leeway and more generally I think it can make sense to sometimes ignore strict adherence to style guides, in this case, for the sake of content/flow.

Either way, it seems my arguments fell short and failed to sway minds! I'll go ahead and change the section titles as requested.

P.S. The same idea regarding await came up in the original PR. Making it a code-block means it will be bolded in the table of contents on the sidebar, so we decided to leave it as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

5 participants