Skip to content

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

@ericsnowcurrently

Description

@ericsnowcurrently

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 fixes3.13bugs and security fixes3.14bugs and security fixesdocsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directory

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions