locking – Python Conditional With Lock Design

locking – Python Conditional With Lock Design

Just use a threading.RLock which 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 RLock is used in the with statements, just like in your sample code:

lock = threading.RLock()

def func1():
    with lock:
        func2()

def func2():
    with lock: # this does not block even though the lock is acquired already
        print hello world

As far as whether or not this is bad design, wed need more context. Why both of the functions need to acquire the lock? When is func2 called by something other than func1?

The Python or is short circuiting so you can make the locking conditional:

def somethingElse(self, hasLock = False):
    #I want this to be conditional...
    with hasLock or self.my_lock:
          print i hate hello worlds

Unfortunately its not quite that easy, because a boolean isnt a valid return from a with statement. Youll need to create a class with the __enter__ and __exit__ to wrap the boolean True value.

Heres one possible implementation that I havent tested.

from contextlib import contextmanager

@contextmanager
def withTrue():
    yield True

def withbool(condition):
    if condition:
        return withTrue()
    return False

def somethingElse(self, hasLock = False):
    with withbool(hasLock) or self.my_lock():
          print i hate hello worlds

This is a lot of boilerplate for something so simple, so the RLock solution looks like a winner. This solution might be useful in a different context though.

locking – Python Conditional With Lock Design

Using with statement is better than just acquire() and release() functions. This way, if an error occurs, the locks will be released.

Leave a Reply

Your email address will not be published. Required fields are marked *