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): # 内部会为当前线程创建一个 ...
随机推荐
- 【转】Qt之JSON保存与读取
简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...
- 自学华为IoT物联网_02 常见物联网通信技术
点击返回自学华为IoT物流网 自学华为IoT物联网_02 常见物联网通信技术 两类技术: 有线通信技术 无线通信技术 一. 有线通信技术 1.1 物联网有线技术介绍及对比 ETH 主要用于支持以太网标 ...
- 基于vue制作简易的柱状图
一般很常见的柱状图,大家都想到用百度echart,如果整个项目就只绘制仅有的一个柱状图,引入echart就有点大材小用了,哈哈哈. 预览地址:https://zuobaiquan.github.io/ ...
- 状压DP总结
状态压缩就是将一行的状态压成一个二进制数,这个数的二进制形式反映了这一行的情况 比如0100111的意义为:这一排的第一个数没被使用,第二个被占用了,第三四个没被占用,第五六七个被占用 我们知道位运算 ...
- sh: /etc/init.d/sshd: not found Docker中的Alpine镜像安装sshd无法启动
问题描述 在Alpine镜像中安装了openssh-server和openssh之后,无法执行ssh localhost.发现未启动服务,开启服务时报以下错误 / # ls /etc/init.d/s ...
- bzoj4869: [Shoi2017]相逢是问候(欧拉函数+线段树)
这题是六省联考的...据说数据还出了点锅,心疼六省选手QAQ 首先要知道扩展欧拉定理... 可以发现每次区间操作都会使模数进行一次phi操作,而一个数最多取logp次phi就会变成1,这时后面的指数就 ...
- Session&&cookie
1.session存在于服务器而cookie存在于客户端: 2.持续时间均为20分钟: 3.session存放的是一个obgect类型,而cookie是string类型: 4.session赋值:Se ...
- 洛谷P3975 弦论
题意:求一个串的字典序第k小的子串/本质不同第k小的子串. 解:一开始我的想法是在后缀树上找,但是不知道后缀树上的边对应的是哪些字符... 然而可以不用fail树转移,用转移边转移即可. 先建一个后缀 ...
- [luogu3294][背单词]
题目链接 题意 读完题目就一个感受:这出题人tm不会说人话吗.真的感觉这个题理解题意比想出正解更难. 其实题目的意思就是,给出一些单词,给这些单词编个号,然后要求其他的单词中是这个单词后缀的词都在这个 ...
- python3+django2 开发易语言网络验证(下)
第六步:网络验证服务器端项目上线部署 功夫不负有心人,终于部署成功啦! 前期准备: 项目名:netauth 系统:百度云服务器下的Ubuntu16.4 软件:xshell(无论如何想办法用这个跟服务器 ...