threading.local作用及原理
先看下应用:
import threading
from threading import local
import time obj = local() def task(i):
obj.xxxxx = i
time.sleep()
print(obj.xxxxx,i) for i in range():
t = threading.Thread(target=task,args=(i,))
t.start()
上述代码实现了线程间的隔离,为每个线程开辟了独立的空间,这就是threading.local的作用
再看下面代码
import time
import threading DIC = {} def task(i): ident = threading.get_ident()
if ident in DIC:
DIC[ident]['xxxxx'] = i
else:
DIC[ident] = {'xxxxx':i }
time.sleep() print(DIC[ident]['xxxxx'],i) for i in range():
t = threading.Thread(target=task,args=(i,))
t.start()
上述代码使用字典实现了和threading.local一样的功能,其实就是threading.local实现的原理。
再看下面代码:
import time
import threading
import greenlet DIC = {} def task(i): # ident = threading.get_ident()
ident = greenlet.getcurrent()
if ident in DIC:
DIC[ident]['xxxxx'] = i
else:
DIC[ident] = {'xxxxx':i }
time.sleep() print(DIC[ident]['xxxxx'],i) for i in range():
t = threading.Thread(target=task,args=(i,))
t.start()
上面代码就是threading.local功能的加强版:支持协程!
接着看:
import time
import threading
try:
import greenlet
get_ident = greenlet.getcurrent
except Exception as e:
get_ident = threading.get_ident class Local(object):
DIC = {} def __getattr__(self, item):
ident = get_ident()
if ident in self.DIC:
return self.DIC[ident].get(item)
return None def __setattr__(self, key, value):
ident = get_ident()
if ident in self.DIC:
self.DIC[ident][key] = value
else:
self.DIC[ident] = {key:value} obj = Local() def task(i):
obj.xxxxx = i
time.sleep()
print(obj.xxxxx,i) for i in range():
t = threading.Thread(target=task,args=(i,))
t.start()
上述代码自己实现了threading.local的数据隔离,并支持协程。
threading.local作用及原理的更多相关文章
- threading.local()使用与原理剖析
threading.local()使用与原理剖析 前言 还是第一次摘出某个方法来专门写一篇随笔,哈哈哈. 为什么要写这个方法呢?因为它确实太重要了,包括后期的Flask框架源码中都有它的影子. 那么我 ...
- 线程锁、threading.local(flask源码中用的到)、线程池、生产者消费者模型
一.线程锁 线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue 线程不安全 + 人(锁) => 排队处理 1.RLock/Lock:一次放一个 a.创建10个线 ...
- 锁,threading local,以及生产者和消费者模型
1.锁:Lock(一次放行一个) 线程安全,多线程操作时,内部会让所有的线程排队处理. 线程不安全:+人=>排队处理 以后锁代码块 v=[] lock=threading.Lock()#声明锁 ...
- 多线程threading.local的作用及原理?
1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...
- threading.local的作用?
threading.local()这个方法的特点用来保存一个全局变量,但是这个全局变量只有在当前线程才能访问,如果你在开发多线程应用的时候 需要每个线程保存一个单独的数据供当前线程操作,可以考虑使用 ...
- flask上下文管理相关 - threading.local 以及原理剖析
threading.local 面向对象相关: setattr/getattr class Foo(object): pass obj = Foo() obj.x1 = 123 # object.__ ...
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- 锁、threading.local、线程池
一.锁 Lock(1次放1个) 什么时候用到锁: 线程安全,多线程操作时,内部会让所有线程排队处理.如:list.dict.queue 线程不安全, import threading import t ...
- 网络编程 多线程/socketserver模块/ threading.local
线程:进程中负责程序执行的执行单元. 多线程:在1个进程中存在多个线程. 进程只是用来把资源集中在一起,而线程才是cpu上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...
随机推荐
- 关系代数(Relation Algebra)与SQL语句的对应关系
SQL语句的执行一般是先翻译为关系代数再被执行的(能有效提高执行速度),所以我们有必要 了解关系代数与SQL语句间的对应关系. 就像高中代数由+-*/和数字组成,关系代数是由union.interse ...
- ControlTemplate in WPF —— ItemsControl
<ItemsControl Margin=" ItemsSource="{Binding Source={StaticResource myTodoList}}"& ...
- 配置pip镜像源(转)
使用pip安装python扩展时,如若没有配置国内镜像源,在未翻墙的情况下,其下载速度将会特别缓慢.因此,有些时候我们必须使用国内镜像源,来解决pip下载安装速度慢的问题,比较常用的国内镜像包括: 阿 ...
- 工具栏对象GUI Status 与GUI Title
GUI Status 与GUI Title用于自定义工具栏按钮及Report程序标题栏显示内容, 可以通过se41\SE80或直接SE38中展开对象列表进行相关操作. 如下是在SE38里,点击[显示物 ...
- C# 二维码、条形码生成
1.工具类:BarCodeHelper(条码生成类),二维码生成类(QRCodeHelper) 2.BarCodeHelper(条码生成类)代码: public class BarCodeHelper ...
- 主机加固之windows2003
这篇与上一篇的win7主机加固内容大体类似,部分有些不同.这篇也可以用来尝试加固windows XP. 1. 配置管理 1.1用户策略 注意:在对Windows系统加固之前先新建一个临时的系统管理员账 ...
- tensorflow 2.0 技巧 | 自定义tf.keras.Model的坑
自定义tf.keras.Model需要注意的点 model.save() subclass Model 是不能直接save的,save成.h5,但是能够save_weights,或者save_form ...
- 【嵌入式开发】Raspberry Pi 树莓派性能测试
Raspberry Pi 树莓派性能测试 目录: CPU Linpack基准测试 源码 编译/运行 结果 Whetstone/Dhrystone综合基准测试 源码 编译/运行 结果 OpenSSL安全 ...
- XSS绕过WAF的姿势
初始测试 1.使用无害的payload,类似<b>,<i>,<u> 观察响应,判断应用程序是否被HTML编码,是否标签被过滤,是否过滤<>等等: 2.如 ...
- YII框架微信公众号
<?phpnamespace backend\controllers; use yii\db\Query;use yii\web\Controller;use Yii;class Exam2Co ...