-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
Expose threading.Semaphore._value #93213
Copy link
Copy link
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Fields
Give feedbackNo fields configured for issues without a type.
Projects
StatusShow more project fields
No status
Feature or enhancement
It would be useful to be able to access the value of
threading.Semaphore._valueas part of the public API ofSemaphore, say via aget_valuemethod.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.Semaphorehas alockedmethod 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 alockedmethod tothreading.Semaphorewould be sufficient for a lot of usage.While writing this issue, I noticed that in 3.11,
threading.Semaphorehas its value printed in__repr__(changed in #20534, the bpo mentions "expose their public states" as the rationale)It looks like
multiprocesing.Semaphorehas an undocumentedget_valuemethod (although there is an issue about potentially removing it #84974 )