python / python/cpython

Clarify the Nature of Python Threads Relative to Other Programming Languages

Offen
#125,927 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

3.12 3.13 3.14 docs stdlib
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Dokumentation des threading-Moduls und prüfe das zugehörige Issue gh-125857 sowie den verknüpften PR gh-125961. Die Arbeit ist abgeschlossen, wenn die Dokumentation kurz erläutert, wie sich das Verhalten von Python bei Daemon-Threads von Thread-Modellen in den meisten anderen Sprachen unterscheidet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
1/5
Geschätzter Aufwand
Unter einer Stunde
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.