Fix deadlocks in cached_property().
This commit is contained in:
parent
a373765d3d
commit
5f1273fd18
1 changed files with 3 additions and 5 deletions
|
|
@ -16,20 +16,18 @@
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import itertools
|
import itertools
|
||||||
|
from collections import defaultdict
|
||||||
from threading import Lock
|
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 = {}
|
locks = defaultdict(Lock)
|
||||||
@functools.wraps(fun)
|
@functools.wraps(fun)
|
||||||
def get(self):
|
def get(self):
|
||||||
with lock:
|
with lock:
|
||||||
try:
|
obj_lock = locks[self, fun]
|
||||||
obj_lock = self._lock
|
|
||||||
except AttributeError:
|
|
||||||
obj_lock = self._lock = Lock()
|
|
||||||
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