Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Clarify the Nature of Python Threads Relative to Other Programming Languages #125927

Copy link
Copy link
Open
@ericsnowcurrently

Description

@ericsnowcurrently
Issue body actions

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

With the exception of Java2 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.

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

Footnotes

  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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirstdlibPython modules in the Lib dirPython modules in the Lib dir

    Projects

    Status

    Todo
    Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.