美文网首页
locked_cached_property

locked_cached_property

作者: 杰克慢 | 来源:发表于2020-01-19 11:04 被阅读0次
    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()
    

    相关文章

      网友评论

          本文标题:locked_cached_property

          本文链接:https://www.haomeiwen.com/subject/pohrzctx.html