• flask上下文管理相关


    threading.local

    面向对象相关: setattr/getattr

    class Foo(object):
    	pass
    
    obj = Foo()
    obj.x1 = 123 
    # object.__setattr__(obj,'x1',123)
    
    print(obj.x1) 
    # object.__getattr__(obj,'x1')
    

    Local类的模拟

    简易版

    class Local(object):
        def __init__(self):
            # self.storage = {}
            object.__setattr__(self,'storage',{})
    
        def __setattr__(self, key, value):
            self.storage[key] = value
    
        def __getattr__(self, item):
            return self.storage[item]
    
    obj = Local()
    
    obj.x1 = 123
    
    print(obj.x1)
    

    升级版

    import threading
    """
    {
        1231:{x1:0},
        1432:{x1:1},
        ...
        5321:{x1:9}
    }
    """
    class Local(object):
        def __init__(self):
            # self.storage = {}
            object.__setattr__(self,'storage',{})
    
        def __setattr__(self, key, value):
            # self.storage[key] = value
            ident = threading.get_ident() # 1233
            if ident in self.storage:
                self.storage[ident][key] = value
            else:
                self.storage[ident] = {key:value}
    
        def __getattr__(self, item):
            ident = threading.get_ident()
            return self.storage[ident][item]
    
    obj = Local()
    
    def task(arg):
        obj.x1 = arg
        print(obj.x1)
    
    for i in range(10):
        t = threading.Thread(target=task,args=(i,))
        t.start()
    

    源码

    class Local(object):
        __slots__ = ("__storage__", "__ident_func__")
    
        def __init__(self):
            object.__setattr__(self, "__storage__", {})
            object.__setattr__(self, "__ident_func__", get_ident)
    
        def __iter__(self):
            return iter(self.__storage__.items())
    
        def __call__(self, proxy):
            """Create a proxy for a name."""
            return LocalProxy(self, proxy)
    
        def __release_local__(self):
            self.__storage__.pop(self.__ident_func__(), None)
    
        def __getattr__(self, name):
            try:
                return self.__storage__[self.__ident_func__()][name]
            except KeyError:
                raise AttributeError(name)
    
        def __setattr__(self, name, value):
            ident = self.__ident_func__()
            storage = self.__storage__
            try:
                storage[ident][name] = value
            except KeyError:
                storage[ident] = {name: value}
    
        def __delattr__(self, name):
            try:
                del self.__storage__[self.__ident_func__()][name]
            except KeyError:
                raise AttributeError(name)
    
  • 相关阅读:
    一些智力题
    17分钟过桥问题
    快排的非递归实现
    单链表逆置
    实现所有括号的合法匹配
    2013阿里笔试题
    Hadoop学习笔记—14.ZooKeeper环境搭建
    Hadoop学习笔记—15.HBase框架学习(基础知识篇)
    Hadoop学习笔记—13.分布式集群中节点的动态添加与下架
    Hadoop学习笔记—11.MapReduce中的排序和分组
  • 原文地址:https://www.cnblogs.com/jjzz1234/p/12037050.html
Copyright © 2020-2023  润新知