Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

gh-145912: Clarify the note about the GIL in the asyncio.to_thread() docs - #162887

Open
ngoldbaum wants to merge 2 commits into
python:mainfrom
ngoldbaum:asyncio-gil-wording
Open

ngoldbaum wants to merge 2 commits into
python:mainfrom
ngoldbaum:asyncio-gil-wording

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This note dates from when asyncio.to_thread() was added in 3.9 (GH-20143), before the free-threaded build existed. Its current content is confusing now that the free-threaded build exists.

The new wording explains that with the GIL enabled, a CPU-bound function in a thread slows the event loop down but does not block it. It also mentions the free-threaded build, where two threads don't have to take turns on a CPU-bound task.

Also see #162886, which this conflicts with. I'll handle dealing with the conflicts.

@read-the-docs-community

read-the-docs-community Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36275822 | 📁 Comparing 92e9309 against main (114f465)

  🔍 Preview build  

1 file changed
± library/asyncio-task.html

Comment on lines +1100 to +1108
``asyncio.to_thread()`` lets the event loop keep running while *func*
runs in another thread. With the :term:`GIL` enabled, the two threads
take turns running Python code, so a CPU-bound function slows the event
loop down. The event loop is not slowed down while the function is in a
call that releases the GIL, which long-running calls into
:term:`native code` often do. With the GIL disabled on the
:term:`free-threaded build`, the two threads do not have to take turns,
and several CPU-bound functions can make progress in
:term:`parallel <parallelism>` with the event loop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could also mention that setting the loop's default executor to an InterpreterPoolExecutor will also bypass the GIL 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants