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

示例

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. Spring Security 5中 PasswordEncoder的使用

    在最新的 Spring Security 5发布版本中, 出于安全性的考虑调整了PasswordEncoder的实现与使用策略. 1.以前常用的实现 StandardPasswordEncoder, ...

  2. Java内存溢出java.lang.OutOfMemoryError: PermGen space

    今天把以前的一个项目部署在tomcat,启动没问题.因为用到了webservice,当调用webservice中的方法时一直报内存溢出异常 Exception in thread "http ...

  3. jQuery 工具类函数-使用$.extend()扩展Object对象

    除使用$.extend扩展工具函数外,还可以扩展原有的Object对象,在扩展对象时,两个对象将进行合并,当存在相同属性名时,后者将覆盖前者,调用格式为: $. extend (obj1,obj2,… ...

  4. Comet OJ - Contest #5

    Comet OJ - Contest #5 总有一天,我会拿掉给\(dyj\)的小裙子的. A 显然 \(ans = min(cnt_1/3,cnt_4/2,cnt5)\) B 我们可以感性理解一下, ...

  5. Trie 树的一些题

    Trie 树的一些题 牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不 ...

  6. python知识点总结01(不定时更新)

    手写一个完整的装饰器模版 # 用于修复被装饰对象的名称空间 from functools import wrape def wrapper(func): @wraps(func) def inner( ...

  7. 洛谷$P2057\ [SHOI2007]$ 善意的投票 网络流

    正解:网络流 解题报告: 传送门! $umm$看到每个人要么0要么1就考虑最小割呗,,,? 然后贡献有两种?一种是违背自己的意愿,一种是和朋友的意愿违背了 所以考虑开一排点分别表示每个人,然后$S$表 ...

  8. php strcmp函数漏洞

    strcmp函数漏洞 适用5.3版本以前的php 函数作用:字符串比较 要求传入字符串.如果传入非字符串呢? 结果函数报错!但是函数返回“0”  . 虽然报错了但函数的判断却是“相等” 如何传入非字符 ...

  9. python版飞机大战代码简易版

    # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import ...

  10. 解决 Table ‘performance_schema.session_variables’ doesn’t exist 问题

    performance_schema在mysql5.5以上就有自带 performance_schema(安装数据库时自带的)如果装数据库或者使用数据时不小心删除了,就会出现Table‘perform ...