Fix memory leak in cached_property.
This commit is contained in:
parent
96d9386e77
commit
f53eeed63b
1 changed files with 5 additions and 2 deletions
|
|
@ -23,11 +23,14 @@ from threading import Lock
|
||||||
def cached_property(fun):
|
def cached_property(fun):
|
||||||
"""A memoize decorator for class properties."""
|
"""A memoize decorator for class properties."""
|
||||||
lock = Lock()
|
lock = Lock()
|
||||||
locks = defaultdict(Lock)
|
|
||||||
@functools.wraps(fun)
|
@functools.wraps(fun)
|
||||||
def get(self):
|
def get(self):
|
||||||
with lock:
|
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:
|
with obj_lock:
|
||||||
try:
|
try:
|
||||||
cache = self._cache
|
cache = self._cache
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue