Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose threading.Semaphore._value #93213

Open
hauntsaninja opened this issue May 25, 2022 · 0 comments
Open

Expose threading.Semaphore._value #93213

hauntsaninja opened this issue May 25, 2022 · 0 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@hauntsaninja
Copy link
Contributor

hauntsaninja commented May 25, 2022

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 #84974 )

@hauntsaninja hauntsaninja added the type-feature A feature request or enhancement label May 25, 2022
hauntsaninja pushed a commit to hauntsaninja/typeshed that referenced this issue May 25, 2022
I've had occasion to use this multiple times. We already have _value for
asyncio.locks.Semaphore See also
python/cpython#93213
hauntsaninja added a commit to python/typeshed that referenced this issue May 25, 2022
I've had occasion to use this multiple times. We already have _value for
asyncio.locks.Semaphore See also
python/cpython#93213

Co-authored-by: hauntsaninja <>
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

2 participants