python / python/cpython

Clarify the Nature of Python Threads Relative to Other Programming Languages

Open
#125,927 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 3.13 3.14 docs stdlib
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

In Python, threads created by threading.Thread are automatically joined during runtime/interpreter finalization, by default. To not auto-join, a programmer must explicitly mark the thread as a "daemon" thread, through Thread(..., daemon=True) or t.daemon = True. Daemon threads keep running until the process exits, at which point the operating system abruptly kills them. [^1]

[^1]: This is known to introduce problems, not just in Python, when a daemon thread tries to use state/memory that has already been cleaned up or freed by the runtime during finalization. Of course, every thread faces the same problem when it relies on references to state/memory but does not control its lifecycle. This requires explicit coordination of the cross-cutting concern. This is certainly just a subset of the general problem of races when using threads.

With the exception of Java[^2] and C#, "daemon" threads are the only kind of thread in pretty much all other programming languages that support threads, so you won't see any mention of "daemon" threads there. Those runtimes do not offer the option to auto-join. Instead, the programmer is responsible for explicitly joining all threads that should block the process from exiting.

[^2]: Java was the inspiration for Python's threading module (in Python 1.5.1, 1998).

For the sake of people familiar with threads in other languages, the threading module docs should briefly state that in most programming languages, "daemon" threads are the only kind of thread and users must always explicitly join.

(Also see gh-125857.)

Linked PRs
  • gh-125961

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the threading module documentation and review the related issue gh-125857 and linked PR gh-125961. The work is done when the docs briefly explain how Python's daemon-thread behavior differs from thread models in most other languages.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.