It is possible to create a count
property for threading._RLock._count
by inheriting from the class and exposing the data from the underlying attribute. This is easily demonstrated by example:
import threading
# noinspection PyProtectedMember
class RLock(threading._RLock):
"""RLock() -> RLock instance with count property"""
@property
def count(self):
"""Count property showing current level of lock ownership."""
return self._count
- Is it possible to do the same with the
_thread.RLock
by getting the count viactypes
? - If it is possible, would the code have any advantages over the version shown above?
- If it would be advantageous, what code would one have to write to access the count?
Yes, it is possible, as
rlockobject
strunct definition is given:yields:
or a more formal version:
Both methods utilize non-public API, it is hard to tell which is better, but I feel the inheriting pure python
RLock
implementation is simpler. The performance difference is neglectable here.