threading.local学习
多线程抢占问题
import time
import threading obj = 5 def task(arg):
global obj
obj = arg
time.sleep(1)
print(obj) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start() # 结果
5
5
5
5
5
5
threading.local对象避免线程抢占
为每个线程开辟一块内存空间,存储各自的数据
import time
import threading obj = threading.local() def task(arg):
global obj
obj.value = arg
time.sleep(1)
print(obj.value) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start() # 结果
0
3
4
5
2
1
模拟threading.local
import time
import threading
# 获取线程的唯一标识
from threading import get_ident class Local():
def __init__(self):
self.storage = {}
def __setitem__(self, key, value):
ident = get_ident()
if ident in self.storage:
self.storage[ident][key] = value
else:
self.storage[ident] = {key:value}
def __getitem__(self, item):
ident = get_ident()
return self.storage[ident][item] obj = Local() def task(arg):
obj['num'] = arg
time.sleep(1)
print(obj['num']) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start()
粒度精确到协程
为每个协程开辟一块内存空间,存储各自的数据
import time
import threading
try:
# 获取协程的唯一标识
from greenlet import getcurrent as get_ident
except Exception as e:
from threading import get_ident class Local():
def __init__(self):
self.storage = {}
def __setitem__(self, key, value):
ident = get_ident()
if ident in self.storage:
self.storage[ident][key] = value
else:
self.storage[ident] = {key:value}
def __getitem__(self, item):
ident = get_ident()
try:
return self.storage[ident][item]
except Exception as e:
return None obj = Local() def task(arg):
obj['num'] = arg
time.sleep(1)
print(obj['num']) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start()
threading.local学习的更多相关文章
- 多线程多进程学习threading,queue线程安全队列,线程间数据状态读取。threading.local() threading.RLock()
http://www.cnblogs.com/alex3714/articles/5230609.html python的多线程是通过上下文切换实现的,只能利用一核CPU,不适合CPU密集操作型任务, ...
- Flask补充--threading.local对象
目录 Local 局部变量 全局变量 使用threading.local() 自定义threading.local 函数版 面向对象版 通过setattr和getattr实现 每个对象有自己的存储空间 ...
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- Threading.local
在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. Threading.local可以创建 ...
- 网络编程 多线程/socketserver模块/ threading.local
线程:进程中负责程序执行的执行单元. 多线程:在1个进程中存在多个线程. 进程只是用来把资源集中在一起,而线程才是cpu上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...
- 多线程局部变量之threading.local()用法
假如,开了十个线程并且做同样的一件事,他们需要带着自己的数据进来,完成事情后带着自己的数据出去.如果是并发,同时进来,他们的数据就会混乱. 一般情况,我们加锁就可以了,一个人先进来,先加锁,另一个人过 ...
- python之threading.local
简述: threading.local是全局变量但是它的值却在当前调用它的线程当中 作用: 在threading module中,有一个非常特别的类local.一旦在主线程实例化了一个local,它会 ...
- [Python 多线程] threading.local类 (六)
在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...
- threading.local()方法;线程池
一,threading.local() import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个 ...
随机推荐
- 「HDU6158」 The Designer(圆的反演)
题目链接多校8-1009 HDU - 6158 The Designer 题意 T(<=1200)组,如图在半径R1.R2相内切的圆的差集位置依次绘制1,2,3,到n号圆,求面积之和(n< ...
- [bzoj2288][pojChallenge]生日礼物【贪心+堆+链表】
题目描述 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, -, AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物. 自然地,ftiasch想要知 ...
- CAN总线要点
前言 CAN总线的应用在现在看来越来越广泛,我厂设备从最初的ARM9与ARM7平台.期间升级过度到CortexA8与Cortex M3平台,再到现在的Cortex M4平台,围绕CAN进行了一系列产品 ...
- ACM-ICPC 2018 南京赛区网络预赛 J题Sum(线性筛素数)
题目链接:https://nanti.jisuanke.com/t/30999 参考自博客:https://kuangbin.github.io/2018/09/01/2018-ACM-ICPC-Na ...
- 网络流24题 gay题报告
洛谷上面有一整套题. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 extra ①飞行员配对方案问题.top 裸二分图匹 ...
- 浅谈 WebDriver如何应对不同浏览器
selenium2 基于对象的测试,在selenium2中一共支持以下浏览器: Firefox(FirefoxDriver) IE(InternetExplorerDriver) Chrome(Chr ...
- 测试利器 Postman
一.安装 官网:https://www.getpostman.com/ Postman是一个Chrome的一个插件工具,我们可以通过Chrome的应用商店进行进行搜索并安装,安装完成会在桌面上显示一个 ...
- 如何测试连接MsSQL数据库-------UDL文件
http://www.xinnet.com/service/cjwt/idc/sjk/1360.html 如果您所使用的 SQL Server 数据库连不上,可以通过这个方法进行测试数据库连接. 温馨 ...
- c/c++ 大于等于 大于 时间效率比较
变成汇编,都是: 大于等于和大于都是电路上的处理,时间上应该差不多.
- springmvc跨域上传文件问题
把以下文件放到webapps的root文件夹下: 1.clientaccesspolicy.xml <?xml version="1.0" encoding="ut ...