how to know python semaphore value

The other answers are correct but for whom reaches this page in order to actually know how to get the semaphore value you can do it like this:

>>> from threading import Semaphore
>>> sem = Semaphore(5)
>>> sem._Semaphore__value
5
>>> sem.acquire()
True
>>> sem._Semaphore__value
4

Be aware that the _Semaphore__ that prefixes the name of the variable value means that this is an implementation detail. Do not write production code based on this variable as it may change in the future. Moreover do not try to edit the value manually, otherwise.. any bad thing can happen.


In python3.6, you can get is like this:

from threading import Semaphore
sem = Semaphore(5)
print(sem._value)

This value is useful for debug


Semaphores are designed around the idea, that threads just grab one, and wait until it becomes available, because it's not really predictable in which order they wil be acquired.

The counter is not part of the abstraction called 'Semaphore'. It is not guaranteed, that your access to the semaphore counter is atomic. If you could peek into the counter, and another thread acquires the semaphore before you do anything with it, what should you do?

Without breaking your code you can't know the value.