先看下应用:

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作用及原理的更多相关文章

  1. threading.local()使用与原理剖析

    threading.local()使用与原理剖析 前言 还是第一次摘出某个方法来专门写一篇随笔,哈哈哈. 为什么要写这个方法呢?因为它确实太重要了,包括后期的Flask框架源码中都有它的影子. 那么我 ...

  2. 线程锁、threading.local(flask源码中用的到)、线程池、生产者消费者模型

    一.线程锁 线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue 线程不安全 + 人(锁) => 排队处理 1.RLock/Lock:一次放一个 a.创建10个线 ...

  3. 锁,threading local,以及生产者和消费者模型

    1.锁:Lock(一次放行一个) 线程安全,多线程操作时,内部会让所有的线程排队处理. 线程不安全:+人=>排队处理 以后锁代码块 v=[] lock=threading.Lock()#声明锁 ...

  4. 多线程threading.local的作用及原理?

    1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...

  5. threading.local的作用?

    threading.local()这个方法的特点用来保存一个全局变量,但是这个全局变量只有在当前线程才能访问,如果你在开发多线程应用的时候  需要每个线程保存一个单独的数据供当前线程操作,可以考虑使用 ...

  6. flask上下文管理相关 - threading.local 以及原理剖析

    threading.local 面向对象相关: setattr/getattr class Foo(object): pass obj = Foo() obj.x1 = 123 # object.__ ...

  7. 自定义threading.local

    1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...

  8. 锁、threading.local、线程池

    一.锁 Lock(1次放1个) 什么时候用到锁: 线程安全,多线程操作时,内部会让所有线程排队处理.如:list.dict.queue 线程不安全, import threading import t ...

  9. 网络编程 多线程/socketserver模块/ threading.local

    线程:进程中负责程序执行的执行单元. 多线程:在1个进程中存在多个线程. 进程只是用来把资源集中在一起,而线程才是cpu上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...

随机推荐

  1. leetcode 分割回文串

    这个方法有问题,这是计算所有子串组成的所有回文子串:而不是所有分割的回文子串: class Solution { public: vector<vector<string>> ...

  2. Tomcat常见启动问题

    1)闪退问题 原因:tomcat软件是java语言开发的. tomcat软件启动时,会默认到系统的环境变量中查找一个名称叫JAVA_HOME的变量.这个变量的作用找到tomcat启动所需的jvm. 解 ...

  3. spingboot之Java邮件发送

    注意: 该项目的工具类可以直接应用于项目 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  4. java:struts框架(网路静态U盘项目)

    1.网络静态U盘项目: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYP ...

  5. Prometheus Querying Function rate() vs irate()

    rate() rate(v range-vector) calculates the per-second average rate of increase of the time series in ...

  6. 面试--hr常问的问题

    程序员换工作,会有技术面试(可能不止一轮的技术面),还会有hr的面试,技术面主要是偏向于技术问题,hr面试主要问的一些问题,下面做下汇总: 1.你换工作的原因,你为何辞职 必问的问题,送分题或者送命题 ...

  7. 超详细的CentOS8Linux新功能介绍 镜像iso下载安装

    在这文章中,我们会使用图解的方式演示 CentOS 8 的安装方法. CentOS8中软件和系统管理请参照https://www.cnblogs.com/fusheng11711/p/11809963 ...

  8. Spring Cloud Zuul Filter 和熔断

    转一篇很不错的关于Spring Cloud Zuul 相关用法的文章,基本包含常用的一些场景,另外附上实际项目中的熔断.打印请求日志和登录验证的实例. 原文地址:https://www.cnblogs ...

  9. springboot - 应用实践(N)使用springboot内置的@Scheduled

    1.springboot开箱即用,内置调度任务的使用. 建一个简单的springboot工程,pom.xml: <?xml version="1.0" encoding=&q ...

  10. BZOJ 1257 余数之和 题解

    题面 这道题是一道整除分块的模板题: 首先,知道分块的人应该知道,n/i最多有2*sqrt(n)种数,但这和余数有什么关系呢? 注意,只要n/i的值和n/(i+d)的值一样,那么n%i到n%(i+d) ...