def with_lock(lock, func, *args, **kwargs):
    """A 'context manager' for performing operations requiring a lock.

    :param lock: A BasicLock instance
    :type lock: silverberg.lock.BasicLock

    :param func: A callable to execute while the lock is held.
    :type func: function
    """
    d = lock.acquire()

    def release_lock(result):
        deferred = lock.release()
        return deferred.addCallback(lambda x: result)

    def lock_acquired(lock):
        return defer.maybeDeferred(func, *args, **kwargs).addBoth(release_lock)

    d.addCallback(lock_acquired)
    return d
def with_lock ( lock , func , * args , ** kwargs ) : d = lock . acquire ( ) def release_lock ( result ) : deferred = lock . release ( ) return deferred . addCallback ( lambda x : result ) def lock_acquired ( lock ) : return defer . maybeDeferred ( func , * args , ** kwargs ) . addBoth ( release_lock ) d . addCallback ( lock_acquired ) return d
A 'context manager' for performing operations requiring a lock.

    :param lock: A BasicLock instance
    :type lock: silverberg.lock.BasicLock

    :param func: A callable to execute while the lock is held.
    :type func: function