a class that assists in keeping methods thread-safe.
This class is intended to be enlisted as a base class to the class being
protected. A method that is to be protected from simultaneous access
would include (at its beginning) a call to _checkLocked():
class MyClass(BaseClass, LockProtected):
def __init__(self, lock=SharedLock):
LockProtected.__init__(self, lock)
...
def dangerous(self):
self._checkLocked()
...
Doing so will require that the protected class be "locked" before a
protected method can be called or else an UnsafeAccessError will be
raised. Locking is done via the with statement:
mc = MyClass()
with mc:
mc.dangerous()
For the locking to work, the protected class must provide to the
LockProtected constructor a lock object. Typically this is a
lsst.utils.multithreading.SharedLock instance or, if one wants to
employ thread notification techniques, a
lsst.utils.multithreading.SharedData instance. It should at least
be a reentrant lock--that is, having the behavior of threading.RLock
(from the standard Python Library).
This class is primarily intended for protecting methods across multiple
classes together via a single lock. That is, it can prevent simultaneous
access to any protected method across multiple class instances. To
accomplish this, each class to be protected should accept a lock as
a constructor argument. Then the same lock is passed into all of the
constructors that need protection together.
Definition at line 31 of file lockProtection.py.