Expose `threading.Semaphore._value`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 36k
- PR merge metrics
- PR metrics pending
Description
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 )
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with threading.Semaphore and its current repr behavior, then compare asyncio.locks.Semaphore.locked with multiprocessing.Semaphore.get_value. Determine whether the public API should expose the value or only its zero state, and consider the existing discussion and rationale before defining tests and documentation for the chosen interface.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100