threding.local
作用:为每一个线程开辟一个独立的内存空间
示例
from threading import Thread, local
import time obj = local() def test(i):
obj.xx = i
time.sleep(2)
print(obj.xx, i) for i in range(10):
t = Thread(target=test, args=(i, ))
t.start()
实现原理
from threading import Thread
import threading
import time #
dic = {} def test(i):
index = threading.get_ident()
if index in dic:
dic[index]['xx'] = i
else:
dic[index] = {'xx': i}
time.sleep(1)
print(dic[index]['xx'], i) for i in range(10):
t = Thread(target=test, args=(i, ))
t.start()
改良
import threading
import time
import greenlet try:
get_ident = greenlet.getcurrent
except Exception as e:
get_ident = threading.get_ident class Local:
dic = {} def __getattr__(self, item):
index = get_ident()
if index in self.dic:
return self.dic[index][item]
else:
return None def __setattr__(self, key, value):
index = get_ident()
if index in self.dic:
self.dic[index][key] = value
else:
self.dic[index] = {key: value} obj = Local() def test(a):
obj.xx = a
time.sleep(2)
print(obj.xx, a) for i in range(10):
t = threading.Thread(target=test, args=(i, ))
t.start()
threding.local的更多相关文章
- sqlalchemy 多线程 创建session
1.基于threding.local,推荐使用 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine ...
- OVS local network 连通性分析 - 每天5分钟玩转 OpenStack(132)
前面已经创建了两个 OVS local network,今天详细分析它们之间的连通性. launch 新的 instance "cirros-vm3",网络选择 second_lo ...
- 再部署一个 instance 和 Local Network - 每天5分钟玩转 OpenStack(131)
上一节部署了 cirros-vm1 到 first_local_net,今天我们将再部署 cirros-vm2 到同一网络,并创建 second_local_net. 连接第二个 instance 到 ...
- 创建 OVS Local Network - 每天5分钟玩转 OpenStack(129)
上一节我们完成了 OVS 的准备工作,本节从最基础的 local network 开始学习.local network 不会与宿主机的任何物理网卡连接,流量只被限制在宿主机内,同时也不关联任何的 VL ...
- Android local.properties 文件读取
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...
- CocoaPods被卡住:Updating local specs repositories
使用CocoaPods被卡住:Updating local specs repositories 使用 pod install --verbose --no-repo-update
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...
- mac,/usr/local is not writable 解决方法
mac,/usr/local is not writable 解决方法 mac 问题 今天在mac上装mongodb,发现提示权限不足问题,错误提示如下: mac安装mongodb错误提示.jpg 尝 ...
- ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' 问题的解决
今天继续作大死,趟php7的配置的坑. 照例,安装了昨天的各种扩展之后,解压php7的压缩文件到 /usr/local/. 然后开始配置config的扩展: ./configure --prefix= ...
随机推荐
- P1082 数列分段
题目描述 对于给定的一个长度为 \(N\) 的正整数数列 \(A_i\) ,现要将其分成连续的若干段,并且每段和不超过 \(M\) (可以等于 \(M\) ),问最少能将其分成多少段使得满足要求. 输 ...
- 浅谈javaScript数据类型、变量、内存之间的关系,文末有图解
一.变量是没有类型的 在JavaScript中,定义变量的方法是“var 变量名=变量值”,无论这个变量要给他赋值为一个数字.字符串还是数组,他的类型都不需要声明.也就是说如果我只声明了一个变量“va ...
- 【30.01%】【hdu 3397】Sequence operation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- Linux 内核即插即用规范
一些新 ISA 设备板遵循特殊的设计规范并且需要一个特别的初始化顺序, 对增加接口板 的简单安装和配置的扩展. 这些板的设计规范称为即插即用, 由一个麻烦的规则集组成, 来建立和配置无跳线的 ISA ...
- You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
异常 You are using the runtime-only build of Vue where the template compiler is not available. Either ...
- __str__、__repr__和__format__
obj.__ str __ ()是面向用户的,该方法将实例转换为一个字符 obj.__ repr __ ()面向程序员,该方法返回一个实例的代码表示形式,通常用来重新构造这个实例,repr()函数返回 ...
- MyBatis原理-注意点
一.${}和#{}的区别 #{}:占位符号,好处防止sql注入 ${}:sql拼接符号 动态 SQL 是 mybatis 的强大特性之一,也是它优于其他 ORM 框架的一个重要原因.mybatis 在 ...
- 高阶函数HOF和高阶组件HOC(Higher Order Func/Comp)
一.什么是高阶函数(组件),作用是什么? 子类使用父类的方法可以通过继承的方式实现,那无关联组件通信(redux).父类使用子类方法(反向继承)呢 为了解决类(函数)功能交叉/功能复用等问题,通过传入 ...
- 「SP25784」BUBBLESORT - Bubble Sort 解题报告
SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...
- 「洛谷P1231」教辅的组成 解题报告
P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...