Rename lock variable, and use it as a context manager.

This commit is contained in:
Gregory Williams
2019-05-15 08:27:20 -07:00
committed by Romain Dorgueil
parent 448ae9ca8d
commit a6473f19ee

View File

@ -146,19 +146,16 @@ class Exclusive(ContextDecorator):
""" """
_locks = {} _locks = {}
lock = threading.Lock() _locks_creation_lock = threading.Lock()
def __init__(self, wrapped): def __init__(self, wrapped):
self._wrapped = wrapped self._wrapped = wrapped
def get_lock(self): def get_lock(self):
_id = id(self._wrapped) _id = id(self._wrapped)
Exclusive.lock.acquire() with Exclusive._locks_creation_lock:
try:
if not _id in Exclusive._locks: if not _id in Exclusive._locks:
Exclusive._locks[_id] = threading.RLock() Exclusive._locks[_id] = threading.RLock()
finally:
Exclusive.lock.release()
return Exclusive._locks[_id] return Exclusive._locks[_id]
def __enter__(self): def __enter__(self):