'Similar to thread_lock, but acquire the lock using try_acquire.
Only release it upon any exception after a successful acquisition.'

@contextlib.contextmanager
def try_thread_lock(self):

    result = None
    try:
        result = self.try_acquire()
        (yield result)
    except:
        if (result is None):
            with excutils.save_and_reraise_exception():
                self.release()
        raise
