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上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...
随机推荐
- electron之API学习
学习一个新框架或者技术,最深入最全面的方法就是通过官方API,例如我们学习electron: 例如我们需要学习electron的BrowserWindow对象的使用,以及在创建她时我们可以配置的参数: ...
- 函数参数<二>
1. 缺省参数 调用函数时,缺省参数的值如果没有传入,则被认为是默认值.下例会打印默认的age,如果age没有被传入: def printinfo( name, age = 35 ): # 打印任何传 ...
- python 提示 :OverflowError: Python int too large to convert to C long
一次在使用orm进行联表查询的时候,出现 Python int too large to convert to C long 的问题: 在分析错误之后,在错误最后面提示中有: File " ...
- CentOS7环境下yum方式安装MySQL5.7
这篇博文主要是从网上摘抄的,做个记录,以后如果有同样的需求,可以直接翻自己的记录.感谢两位大神: https://www.cnblogs.com/luohanguo/p/9045391.html ht ...
- flask包request获取参数
原博文:https://www.cnblogs.com/wangjikun/p/6935592.html request.method #获取请求方法request.form #获取post请求所有参 ...
- 调整Linux终端显示分辨率
linux 默认cli (command line interface)分辨率一般都比较小,显示的字体很大,不太美观,有时还影响结果的显示(例如出现kernel panic).所以有必要改变一下cli ...
- Selenium 2自动化测试实战13(设置元素等待)
一.设置元素等待 若在加载某个元素时延迟而造成的ElementNotVisbleException的情况出现,那么就会降低自动化脚本的稳定性,可以通过设置元素等待改善这种问题造成的不稳定. webdr ...
- nagios客户端安装监控
1.在客户端机器上安装epel扩展源 yum install -y epel-release2.yum安装nagios及依赖包软件 yum install -y nagios-plugins nagi ...
- Matlab学习笔记0—课程导入
0,Matlab语言的介绍 1.什么叫计算? 在汉语中,“计算”一词的含义: 谋划 ,考虑 , 算计.随着电子计算机的产生与应用,人们对“计算”的理解发生了很大的变化. (1) ...
- P3367 【模板】并查集
喵呜~~(题面) 这题其实很早就过了,但是呢,以前过的时候真的基本上是CtrlC+CtrlV,这次把代码重新码了一遍,对并查集也有了一个基本清晰的认识 #include<iostream> ...