import threading try: from greenlet import getcurrent as get_ident # 协程 except ImportError: try: from thread import get_ident except ImportError: from _thread import get_ident # 线程 class Local(object): def __init__(self): object.__setattr__(self, '__storage__', {}) object.__setattr__(self, '__ident_func__', get_ident) 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) local_values = Local() def task(num): local_values.name = num import time time.sleep(1) print(local_values.name, threading.current_thread().name) for i in range(20): th = threading.Thread(target=task, args=(i,),name='线程%s' % i) th.start()
thread local变量
原创
©著作权归作者所有:来自51CTO博客作者zhaichaoqun的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:开发自定义Mysql连接池
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
linux-local.repo 搭建
linux本地repo 搭建
CentOS repo -
Java Thread Dump文件分析
Java Thread Dump文件分析
java 堆栈 Java -
thread_local 变量的析构顺序
thread_local变量的析构顺序
linux g++ #include c++11 -
C++11新特性:thread_local
threadSpecificCounter是线程局部存储的变量,每个线程都有自己的拷贝,每个线程都会递增它,并模拟一些计算工作,同时更
c++ 算法 开发语言 #include 局部存储