python / python/cpython

Clarify the Nature of Python Threads Relative to Other Programming Languages

Aperta
#125,927 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.12 3.13 3.14 docs stdlib
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla documentazione del modulo threading e esamina l’issue correlata gh-125857 e la PR collegata gh-125961. Il lavoro è completato quando la documentazione spiega brevemente in che modo il comportamento dei daemon thread di Python differisce dai modelli di thread della maggior parte degli altri linguaggi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.