python / python/cpython

Expose `threading.Semaphore._value`

Offen
#93,213 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beschreibung

Feature or enhancement

It would be useful to be able to access the value of threading.Semaphore._value as part of the public API of Semaphore, say via a get_value method.

Pitch

The value is useful for debugging and testing, helping users to understand the state and invariants of their code. There is also educational value in allowing users to see the internal state of a Semaphore, seeing as how it's an important concurrency primitive.

https://cs.github.com/?scopeName=All+repos&scope=&q=semaphore._value+language%3Apython# finds 68 uses of the exact phrase "semaphore._value" on Github, so I'm not alone in wanting access to this (although several of these are asyncio.locks.Semaphore).

Previous discussion

asyncio.locks.Semaphore has a locked method that exposes whether the value of the Semaphore is zero. It does appear that many of the uses are comparing the value to zero, so adding a locked method to threading.Semaphore would be sufficient for a lot of usage.

While writing this issue, I noticed that in 3.11, threading.Semaphore has its value printed in __repr__ (changed in #20534, the bpo mentions "expose their public states" as the rationale)

It looks like multiprocesing.Semaphore has an undocumented get_value method (although there is an issue about potentially removing it https://github.com/python/cpython/issues/84974 )

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 threading.Semaphore und seinem aktuellen repr-Verhalten und vergleiche dann asyncio.locks.Semaphore.locked mit multiprocessing.Semaphore.get_value. Bestimme, ob die öffentliche API den Wert oder nur den Zustand, ob er null ist, offenlegen sollte, und berücksichtige die bestehende Diskussion und Begründung, bevor du Tests und Dokumentation für die ausgewählte Schnittstelle definierst.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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