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

示例

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. springboot2.x整合JPA

    项目结构 pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  2. fetch是什么?写一个fetch请求

    fetch是web提供的一个可以获取异步资源的api,目前还没有被所有浏览器支持,它提供的api返回的是Promise对象,所以你在了解这个api前首先得了解Promise的用法. 参考链接:http ...

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

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

  4. js简单实现promise

    function myPromise(fn){ let status='pending',successCallback=[],failedCallback=[],data=null,reason=n ...

  5. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  6. js解决跨域下载文件

    之前用的是a标签的方式,同源是没有问题的,但一跨域就不行了,试了其它方法,不是报跨域错误,就是在当前页面打开文件,体验相当不好. data = data.replace(/\\/g, '/'); va ...

  7. mac系统上访问docker容器中的ip配置

    使用 mac系统,发现docker没有 docker0网桥,无法直接在宿主机上 访问 容器的ip, 在测试的时候有这种需求,而不是通过-p的方式,可以参考下面的连接,主要就是 修改 setting.j ...

  8. 深入Oracle的left join中on和where的区别详解

    -- from http://blog.itpub.net/30175262/viewspace-1472060/ 今天遇到一个求某月所有天数的统计结果,如果某日的结果是0也需要显示出来,即: 日期 ...

  9. linux_jdk,zookeeper,kafka安装

    若是在虚拟机安装最好先进行下面的安装 1.1修改各个虚拟机主机名 vi /etc/sysconfig/network 1.2修改主机名和IP的映射关系 vi /etc/hosts 1.3关闭防火墙 # ...

  10. 阿里巴巴java开发手册学习记录,php版

    一.编程规约 (一)命名风格 1.目录使用小写+下划线 home,view,model,admin_view 2.类 UpperCamelCase PhpMailer方法 lowerCamelCase ...