Skip to content

gh-145912: Add a multi-threaded programming HOWTO - #162624

Draft
ngoldbaum wants to merge 6 commits into
python:mainfrom
ngoldbaum:multi-threading-howto
Draft

ngoldbaum wants to merge 6 commits into
python:mainfrom
ngoldbaum:multi-threading-howto

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #145912.

Adds a practical introduction to writing correct multi-threaded Python code, covering race conditions, sharing data, and coordinating threads on both default and free-threaded builds.

Opening as a draft for feedback on the scope and structure and to get a docs preview; several sections are still outlines.

@read-the-docs-community

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

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36277132 | 📁 Comparing 4e291f9 against main (4b66b87)

  🔍 Preview build  

41 files changed · + 1 added · ± 40 modified

+ Added

± Modified

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36164738 | 📁 Comparing 049fd33 against main (4b66b87)

  🔍 Preview build  

3 files changed
+ howto/multi-threading.html
± glossary.html
± howto/index.html

@willingc willingc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ngoldbaum This draft is off to a fabulous start. A few suggestions to ground readers before diving into specifics. Thank you!

Comment thread Doc/howto/multi-threading.rst Outdated
Comment on lines +12 to +13
:class:`concurrent.futures.ThreadPoolExecutor`. Each thread runs its own
function :term:`concurrently <concurrency>` with the others.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
:class:`concurrent.futures.ThreadPoolExecutor`. Each thread runs its own
function :term:`concurrently <concurrency>` with the others.
:class:`concurrent.futures.ThreadPoolExecutor`. Each thread works :term:`concurrently <concurrency>` with the others.

Comment thread Doc/howto/multi-threading.rst Outdated
Comment on lines +18 to +19
appear to freeze because it is doing other work and cannot check for user
input. When a function makes its caller wait for an arbitrarily long time, it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
appear to freeze because it is doing other work and cannot check for user
input. When a function makes its caller wait for an arbitrarily long time, it
appear to freeze while doing other work and unable to check for user
input. When a function makes its caller wait for an arbitrarily long time, it

with ``async`` and ``await``; threads do it for ordinary functions that may
block.

Threads can also divide work between CPU cores. In the default build of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A new heading would be helpful here. The first two paragraphs are introductory and address basic terms. This paragraph starts the difference between traditional GIL version and free-threaded build.

Subheadings for Sharing modules and globals, Sharing objects, and Thread safety would be helpful for scanning and grounding the reader's context.

cost?
- How do I coordinate work between the threads in my program?

This HOWTO is for Python programmers who are new to writing multi-threaded

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would add a heading here. "Writing multi-threaded code" or something similar.

classes, and it does not assume any experience with threads.

Below, we describe several strategies for making Python code
thread-safe. Working with threads requires a trade-off between safety,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider bolding the trade-offs: safety, performance, code simplicity

@bedevere-app bedevere-app Bot mentioned this pull request Nov 30, 2026
18 of 49 tasks
@ngoldbaum

ngoldbaum commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @willingc I think I integrated your comments. Instead of bolding things for emphasis, I linked to a table that illustrates the trade-offs.

I also added an outline for a "why should I use threads" section, which I think this needs and was missing.

I also opened two other PRs related to this work:

Once those are reviewed separately I'll update this to link to text from those PRs where appropriate. I expect there to be more general docs improvements spawned by this work. I'll be at the docs team meeting tomorrow to get a temperature check on this approach.

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

Labels

docs Documentation in the Doc dir skip news topic-free-threading

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants