线程threading.local类源码解读

本文是个人阅读的结果,如有差错,请告知。

threading.local类的作用:为不同线程创造同名,但是不同的作用域的变量,使用起来不会互相影响。

from weakref import ref  # ref用在了构造大字典元素元组的第一个位置即 (ref(Thread), 线程字典)
from contextlib import contextmanager  # 上下文管理,用来确保__dict__属性的存在
from threading import current_thread, RLock
__all__ = ["local"]

class _localimpl:  # local()._local__impl = _localimpl()  # local()实例的属性_local__impl就是这个类的实例
    """一个管理线程字典的类"""
    __slots__ = 'key', 'dicts', 'localargs', 'locallock', '__weakref__'  # _local__impl有这么多属性

    def __init__(self):
        # 这个self.key是用在线程对象的字典中的key
        # self.key使用的一个字符串,这样既能运行的快,
        # 但是通过'_threading_local._localimpl.' + str(id(self)也能保证不会冲突别的属性

        self.key = '_threading_local._localimpl.' + str(id(self))
        #
        self.dicts = {}  # 大字典
        # 格式是: { id(线程1):(ref(Thread), 线程1自身的字典), id(线程2):(ref(Thread), 线程2自身的字典), ... }

    def get_dict(self):  # 从大字典中拿(ref(Thread), 线程字典), 然后取线程字典
        thread = current_thread()
        return self.dicts[id(thread)][1]

    def create_dict(self):  # 为当前线程创建一个线程字典,就是(ref(Thread), 线程字典)[1],即元组的第二部分
        localdict = {}
        key = self.key  # key使用'_threading_local._localimpl.' + str(id(self)
        thread = current_thread()  # 当前线程
        idt = id(thread)  # 当前线程的id
        def local_deleted(_, key=key):  # 这个函数不看  pass
            # When the localimpl is deleted, remove the thread attribute.
            thread = wrthread()
            if thread is not None:
                del thread.__dict__[key]
        def thread_deleted(_, idt=idt):  # 这个函数不看 pass
            # When the thread is deleted, remove the local dict.
            # Note that this is suboptimal if the thread object gets
            # caught in a reference loop. We would like to be called
            # as soon as the OS-level thread ends instead.
            local = wrlocal()
            if local is not None:
                dct = local.dicts.pop(idt)
        wrlocal = ref(self, local_deleted)
        wrthread = ref(thread, thread_deleted)  # 大字典中每一个线程对应的元素的第一个位置: (ref(Thread), 小字典)
        thread.__dict__[key] = wrlocal
        self.dicts[idt] = wrthread, localdict  # 在大字典中构造: id(thread) : (ref(Thread), 小字典)
        return localdict


@contextmanager
def _patch(self):
    impl = object.__getattribute__(self, '_local__impl')  # 此时的self是local(), 拿local()._local__impl
    try:
        dct = impl.get_dict()   # 然后从拿到的local()._local__impl调用线程字典管理类的local()._local__impl.get_dict()方法
                                # 从20行到22这个get_dict()方法的定义可以看出来,拿不到会报KeyError的

    except KeyError:  # 如果拿不到报 KeyError之后捕捉
        dct = impl.create_dict()  # 然后再通过线程字典管理类临时创建一个
        args, kw = impl.localargs  # 这个时候把拿到
        self.__init__(*args, **kw)
    with impl.locallock:  # 通过上下文的方式上锁
        object.__setattr__(self, '__dict__', dct)  # 给local() 实例增加__dict__属性,这个属性指向大字典中value元组的第二个元素,即线程小字典
        yield  # 到目前为止,local()类的两个属性都构造完成


class local:  # local类
    __slots__ = '_local__impl', '__dict__'  # local类有两个属性可以访问

    def __new__(cls, *args, **kw):
        if (args or kw) and (cls.__init__ is object.__init__):  # pass不看
            raise TypeError("Initialization arguments are not supported")
        self = object.__new__(cls)  # pass不看
        impl = _localimpl()  # _local_impl属性对应的是_localimpl类的实例
        impl.localargs = (args, kw)  # _local_impl属性即_localimpl类的实例 的 localargs属性是一个元组
        impl.locallock = RLock()  # pass 不看
        object.__setattr__(self, '_local__impl', impl)
        # 把_local__impl 增加给local(), 所以:local()._local__impl is ipml 即 _localimp()

        # __slots__规定了local()有两个属性,这里已经设置了一个_local__impl;
        # 第二个属性__dict__当我们以后在访问的时候使用上下文进行临时增加,比如第85行

        impl.create_dict()  # 就是local._local__impl.create_dict()
        return self  # 返回这个配置好_local__impl属性的local()实例

    def __getattribute__(self, name):  # 当我们取local()的属性时
        with _patch(self):  # 会通过上下文先把数据准备好
            return object.__getattribute__(self, name)  # 在准备好的数据中去拿要拿的属性name

    def __setattr__(self, name, value):
        if name == '__dict__':  # 这个判断语句是控制local()实例的__dict__属性只能读不能被替换
            raise AttributeError(
                "%r object attribute '__dict__' is read-only"
                % self.__class__.__name__)
        with _patch(self):  # 同理, 通过上下文先把__dict__构造好
            return object.__setattr__(self, name, value)  # 然后调用基类的方法设置属性

    def __delattr__(self, name):  # 删除属性,同理,和__setattr__手法相似
        if name == '__dict__':   # 这个判断语句是控制local()实例的__dict__属性只能读不能被替换
            raise AttributeError(
                "%r object attribute '__dict__' is read-only"
                % self.__class__.__name__)
        with _patch(self):  # 同理, 通过上下文先把__dict__构造好
            return object.__delattr__(self, name)

# 整体架构图:
'''

                                                                               / ——  key 属性
                                                                              /  ——  dicts 属性, 格式{id(Thread):(ref(Thread), 线程小字典)}
                        ———— : _local__impl属性   ----------  是_local类的实例                                                     |
                      /                                                          —— 其他属性...                                    |
                     /                          /—————————————————————————————————————————————————————————————————————————————————/
    创建一个local实例                           /
                     \                        /
                      \                     /
                        ———— : __dict__属性  --------  对应的是_local__impl属性的dicts 中的线程小字典



'''
发布了70 篇原创文章 · 获赞 45 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38727847/article/details/102995310