I am trying to do some shared locking using with statements
def someMethod(self, hasLock = False):
with self.my_lock:
self.somethingElse(hasLock=True)
def somethingElse(self, hasLock = False):
#I want this to be conditional...
with self.my_lock:
print 'i hate hello worlds"
That make sense? I basically only want to do the with if I don’t already have the lock.
On top of being able to accomplish this, is it a bad design? Should I just acquire/release myself?
Just use a
threading.RLockwhich is re-entrant meaning it can be acquired multiple times by the same thread.http://docs.python.org/library/threading.html#rlock-objects
For clarity, the
RLockis used in thewithstatements, just like in your sample code:As far as whether or not this is bad design, we’d need more context. Why both of the functions need to acquire the lock? When is
func2called by something other thanfunc1?