作用:为每一个线程开辟一个独立的内存空间

示例

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的更多相关文章

  1. sqlalchemy 多线程 创建session

    1.基于threding.local,推荐使用 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine ...

  2. OVS local network 连通性分析 - 每天5分钟玩转 OpenStack(132)

    前面已经创建了两个 OVS local network,今天详细分析它们之间的连通性. launch 新的 instance "cirros-vm3",网络选择 second_lo ...

  3. 再部署一个 instance 和 Local Network - 每天5分钟玩转 OpenStack(131)

    上一节部署了 cirros-vm1 到 first_local_net,今天我们将再部署 cirros-vm2 到同一网络,并创建 second_local_net. 连接第二个 instance 到 ...

  4. 创建 OVS Local Network - 每天5分钟玩转 OpenStack(129)

    上一节我们完成了 OVS 的准备工作,本节从最基础的 local network 开始学习.local network 不会与宿主机的任何物理网卡连接,流量只被限制在宿主机内,同时也不关联任何的 VL ...

  5. Android local.properties 文件读取

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...

  6. CocoaPods被卡住:Updating local specs repositories

    使用CocoaPods被卡住:Updating local specs repositories 使用 pod install --verbose --no-repo-update

  7. 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 * ...

  8. mac,/usr/local is not writable 解决方法

    mac,/usr/local is not writable 解决方法 mac 问题 今天在mac上装mongodb,发现提示权限不足问题,错误提示如下: mac安装mongodb错误提示.jpg 尝 ...

  9. ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' 问题的解决

    今天继续作大死,趟php7的配置的坑. 照例,安装了昨天的各种扩展之后,解压php7的压缩文件到 /usr/local/. 然后开始配置config的扩展: ./configure --prefix= ...

随机推荐

  1. Mail.Ru Cup 2018 Round 2 B. Alice and Hairdresser (bitset<> or 其他)

    传送门 题意: 给出你序列 a,在序列 a 上执行两种操作: ① 0 :查询有多少连续的片段[L,...,R],满足 a[L,...,R] > l: ② 1 p d :将第 p 个数增加 d: ...

  2. asp dotnet core 支持客户端上传文件

    本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...

  3. C# 文件在数据库 的 存取

    ... /// <summary> /// 获取数据库Image字段数据,保存到本地 /// </summary> /// <param name="sende ...

  4. 牛客小白月赛15A 斑羚飞渡

    链接:https://ac.nowcoder.com/acm/contest/917/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64b ...

  5. sqlyog提示:没有执行查询。请在 SQL 窗口中输入查询或将光标放在一个查询上。

    以下内容纯属猜测: sqlyog 在执行输入的sql语句时,对已经执行过的语句,是不会再执行的,所以出现这个问题,很可能是sql语句没有修改,就试图执行第二遍.

  6. HDU4609 FFT+组合计数

    HDU4609 FFT+组合计数 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 找出n根木棍中取出三根木棍可以组成三角形的概率 题解: ...

  7. 【Docker】初识与应用场景认知

    什么是Docker? Docker是一个容器化平台,它以容器的形式将您的应用程序及其所有依赖项打包在一起,以确保您的应用程序在任何环境中无缝运行. 什么是Docker容器? Docker容器包括应用程 ...

  8. 赋值、关系、逻辑运算、if、switch case

    cout << boolalpha将打印输出0转为false,1转为true 逻辑运算符: &&(与)    且      两个条件为真时结果为真 ||(或) 或   两个 ...

  9. 第三阶段:3.Web端产品设计:3.产品设计的层次

    一般淘宝购物的五个流程:浏览对应表现层,搜索对应框架层,产品页面对应架构层,确认对应范围层,付款对应战略层. 从具象到抽象.但是我们做产品都是从抽象到具象,而用户使用产品是从具象到抽象. 各个层次之间 ...

  10. 基于python的二分搜索和例题

    二分搜索 二分概念 二分搜索是一种在有序数组中查找某一特定元素的搜索算法. 搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束: 如果某一特定元素大于或者小于中间元素,则在数 ...