Threading.local
在多线程环境下,每个线程都有自己的数据。一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁。
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的更多相关文章
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- threading.local学习
多线程抢占问题 import time import threading obj = 5 def task(arg): global obj obj = arg time.sleep(1) print ...
- 网络编程 多线程/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): # 内部会为当前线程创建一个 ...
- 多线程threading.local的作用及原理?
1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...
- 线程锁、threading.local(flask源码中用的到)、线程池、生产者消费者模型
一.线程锁 线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue 线程不安全 + 人(锁) => 排队处理 1.RLock/Lock:一次放一个 a.创建10个线 ...
随机推荐
- MyBatis-resultType 几种返回类型
一.返回集合 1.返回JavaBean集合 public List<MyUser> selectMyUserByNameLike(String name); <!-- resultT ...
- ffmpeg的各种黑科技
获取音频的时长 /** * 获取视频文件的时长 * @param ffmpegPath 是ffmpeg软件存放的目录,sourceFile是目标文件 * @return */ public Strin ...
- uby on rails 用户密码加密
运行环境: rails 4.2.1 ruby 2.0.0p481 mysql(支持多种数据库) 在实际的项目中,需要注意对用户 ...
- ELASTICSEARCH 搜索的评分机制
从我们在elasticsearch复合框输入搜索语句到结果显示,展现给我们的是一个按score得分从高到底排好序的结果集.下面就来学习下elasticsearch怎样计算得分. Lucene(或 El ...
- python中的顺序表
Python中的list和tuple两种类型采用了顺序表的实现技术,tuple是不可变类型,即不变的顺序表,因此不支持改变其内部状态的任何操作,而其他方面,则与list的性质类似. list的基本实现 ...
- 22.doublewrite/ChangeBuffer/AHI/FNP
一. Double Write1 Double Write介绍 • Double Write的目的是为了保证数据写入的可靠性, 避免partial write 的情况 ◦ partial write( ...
- Oracle SQL*Plus命令
登录数据库: 方式(1)当我们刚安装Oracle数据库时,登录账户时可以使用win+r 输入sqlplus,进入sqlplus命令窗口,然后输入用户名和密码,这里输入密码时不会有回显 方式(2)使用w ...
- webxml样板
<!--<!DOCTYPE web-app PUBLIC--> <!--"-//Sun Microsystems, Inc.//DTD Web Application ...
- 隐马尔可夫模型HMM(一)
摘自 1.李航的<统计学习方法> 2.https://www.cnblogs.com/pinard/p/6945257.html 了解HMM模型 1.隐马尔可夫模型的定义 隐马尔可夫模型是 ...
- SaltStack 理解
一.SaltStack 原理: 1.SaltStack 也是基于CS模式的主控master和client被控端 minion 结构:也是一个异构平台基础设置管理工具:遵守Apache2协议,完全开源. ...