在多线程环境下,每个线程都有自己的数据。一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁。

Threading.local可以创建一个对象,每个线程都可以对他读写属性,但不会互相影响

import threading
import time
# 创建全局ThreadLocal对象:
class A:
pass
# local_school = A()
local_school = threading.local() def process_student():
print('Hello, %s (in %s)' % (local_school.student, threading.current_thread().name)) def process_thread(name):
# 绑定ThreadLocal的student: local_school.student = name
time.sleep(2)
process_student()
a = []
for i in range(20):
a.append(threading.Thread(target= process_thread, args=(str(i),), name=str(i))) for i in a:
i.start()

通过字典以及面向对象中的魔法方法来自己实现一个

import time
from threading import get_ident,Thread
class PPP:
def __init__(self):
object.__setattr__(self,"storage", {})
def __setattr__(self, key, value):
if get_ident() in self.storage:
self.storage[get_ident()][key]=value
else:
self.storage[get_ident()] ={key: value}
def __getattr__(self, item):
return self.storage[get_ident()][item] p =PPP()
def task(arg):
p.a = arg
time.sleep(2)
print(p.a) for i in range(10):
t = Thread(target=task,args=(i,))
t.start()

Threading.local的更多相关文章

  1. 自定义threading.local

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

  2. threading.local学习

    多线程抢占问题 import time import threading obj = 5 def task(arg): global obj obj = arg time.sleep(1) print ...

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

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

  4. 多线程局部变量之threading.local()用法

    假如,开了十个线程并且做同样的一件事,他们需要带着自己的数据进来,完成事情后带着自己的数据出去.如果是并发,同时进来,他们的数据就会混乱. 一般情况,我们加锁就可以了,一个人先进来,先加锁,另一个人过 ...

  5. python之threading.local

    简述: threading.local是全局变量但是它的值却在当前调用它的线程当中 作用: 在threading module中,有一个非常特别的类local.一旦在主线程实例化了一个local,它会 ...

  6. [Python 多线程] threading.local类 (六)

    在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...

  7. threading.local()方法;线程池

    一,threading.local() import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个 ...

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

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

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

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

随机推荐

  1. elasticsearch 集成springboot

    和jpa类似,很简单,很强大. pom <dependencies> <dependency> <groupId>org.springframework.boot& ...

  2. python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验

    一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...

  3. python 面向对象(二)成员

    ##################################总结########################### 类的成员: 变量: 实例变量      对象.属性=xxx 类变量    ...

  4. HDU 1050(搬椅子 数学)

    题意是在一个有 400 个房间的走廊中搬动房间里的椅子,如果两次的路线重叠,就要分两次搬动,如果不重叠,就可以一次搬动. 开始的时候直接当成求线段重叠条数的题,发现这种思路完全是错的,比如 1 - 3 ...

  5. 使用模拟Table解决span重叠问题

    <div id="test"> <div > <span>adfafadsfadfa</span> <span style=& ...

  6. IDApython教程(四)

    前三部分已经验证了用IDAPython能够让工作变的更简单,这一部分让我们看看逆向工程师如何使用IDAPython的颜色和强大的脚本特性. 分析者经常需要面对越来越复杂的代码,而且有时候无法轻易看出动 ...

  7. 【noip 2015】普及组

    T1.金币 题目链接 #include<cstdio> #include<algorithm> #include<cstring> using namespace ...

  8. Debian Security Advisory(Debian安全报告) DSA-4410-1 openjdk-8 security update

    Debian Security Advisory(Debian安全报告) DSA-4410-1 openjdk-8 security update Package :openjdk-8 CVE ID: ...

  9. python的基础初始第二天

    1.基础数据类型初始 1,数字类型,int,用于计算,+ ,- ,*, /,加,减,乘,除.在python2有整型和长整型之分(3344L),在python3 已经不区分了. 2,字符串类型strin ...

  10. linux sqlite replace into

    sqlite replace into 文档详细说明: http://blog.sina.com.cn/s/blog_590be5290102vulh.html 这点很重要: 一般用replace语句 ...