Fix memory leak in cached_property.

This commit is contained in:
Andrey Golovizin 2014-09-02 16:49:19 +02:00
parent 96d9386e77
commit f53eeed63b

View file

@ -23,11 +23,14 @@ from threading import Lock
def cached_property(fun):
"""A memoize decorator for class properties."""
lock = Lock()
locks = defaultdict(Lock)
@functools.wraps(fun)
def get(self):
with lock:
obj_lock = locks[self, fun]
try:
obj_locks = self._locks
except AttributeError:
obj_locks = self._locks = defaultdict(Lock)
obj_lock = obj_locks[fun]
with obj_lock:
try:
cache = self._cache