from threading import RLock
class locked_cached_property(object):
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
self.lock = RLock()
def __get__(self, obj, type=None):
if obj is None:
return self
with self.lock:
value = obj.__dict__.get(self.__name__, "")
if value is "":
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
def p():
print("p")
def p1():
print("p")
class Test(object):
@locked_cached_property
def jinja_loader(self):
return p
@locked_cached_property
def jinja_loader1(self):
return p1
a = Test()
a.jinja_loader()
a.jinja_loader1()