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

示例

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. UWP IRandomAccessStream 与 Stream 互转

    本文告诉大家如何将 IRandomAccessStream 和 Stream 互转 如果在使用网络传输文件的时候,在 UWP 经常使用将 IRandomAccessStream 和 Stream 互转 ...

  2. H3C端口状态迁移

  3. antd Bug记录

    antd-mobile Carousel 走马灯竖向滚动内容为空会导致visibility:hidden; Carousel Banner轮播组件初始化加载高度不正确可以在第一张图片onload事件的 ...

  4. 2018-11-13-WPF-禁用实时触摸

    title author date CreateTime categories WPF 禁用实时触摸 lindexi 2018-11-13 10:45:37 +0800 2018-5-4 21:0:3 ...

  5. vue 插件大全

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  6. 【47.63%】【hdu 1532】Drainage Ditches

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  7. Elasticsearch介绍和安装

    Elasticsearch介绍和安装 软件包: 链接:https://pan.baidu.com/s/1O_C0JQGfF8sC_OtcCCLNoQ 提取码:3iai 1.1.简介 1.1.1.Ela ...

  8. VRChat之转移地图缓存

    我的电脑是win10,win10的缓存地址和名称可能和win7的名字有所不同. win10缓存路径:C:\Users\Administrator\AppData\LocalLow\VRChat\VRC ...

  9. Visio图表应用

    两种方式创建 插入图表之后通过设计面板的更改图表类型进行更改 双击图表进入编辑状态. 组合其中组合图的利用 下面是图表编辑操作: 双击进入图表之后下方会有“Chart1”跟“Sheet1”两个板块 而 ...

  10. javascript DOM 编程艺术 札记1

    一个重要观点 DOM 是指 文档对象模型,它对应浏览器实际认知的东西.html 文本本身和 html 加载到浏览器中显示的东西并不是完全一致的,后者就是 DOM 节点树,它是浏览器实际认知的东西.一个 ...