threading.local学习
多线程抢占问题
import time
import threading obj = 5 def task(arg):
global obj
obj = arg
time.sleep(1)
print(obj) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start() # 结果
5
5
5
5
5
5
threading.local对象避免线程抢占
为每个线程开辟一块内存空间,存储各自的数据
import time
import threading obj = threading.local() def task(arg):
global obj
obj.value = arg
time.sleep(1)
print(obj.value) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start() # 结果
0
3
4
5
2
1
模拟threading.local
import time
import threading
# 获取线程的唯一标识
from threading import get_ident class Local():
def __init__(self):
self.storage = {}
def __setitem__(self, key, value):
ident = get_ident()
if ident in self.storage:
self.storage[ident][key] = value
else:
self.storage[ident] = {key:value}
def __getitem__(self, item):
ident = get_ident()
return self.storage[ident][item] obj = Local() def task(arg):
obj['num'] = arg
time.sleep(1)
print(obj['num']) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start()
粒度精确到协程
为每个协程开辟一块内存空间,存储各自的数据
import time
import threading
try:
# 获取协程的唯一标识
from greenlet import getcurrent as get_ident
except Exception as e:
from threading import get_ident class Local():
def __init__(self):
self.storage = {}
def __setitem__(self, key, value):
ident = get_ident()
if ident in self.storage:
self.storage[ident][key] = value
else:
self.storage[ident] = {key:value}
def __getitem__(self, item):
ident = get_ident()
try:
return self.storage[ident][item]
except Exception as e:
return None obj = Local() def task(arg):
obj['num'] = arg
time.sleep(1)
print(obj['num']) for i in range(6):
t = threading.Thread(target=task, args=(i,))
t.start()
threading.local学习的更多相关文章
- 多线程多进程学习threading,queue线程安全队列,线程间数据状态读取。threading.local() threading.RLock()
http://www.cnblogs.com/alex3714/articles/5230609.html python的多线程是通过上下文切换实现的,只能利用一核CPU,不适合CPU密集操作型任务, ...
- Flask补充--threading.local对象
目录 Local 局部变量 全局变量 使用threading.local() 自定义threading.local 函数版 面向对象版 通过setattr和getattr实现 每个对象有自己的存储空间 ...
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- Threading.local
在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. Threading.local可以创建 ...
- 网络编程 多线程/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): # 内部会为当前线程创建一个 ...
随机推荐
- Android 性能优化提示
原文 http://developer.android.com/guide/practices/design/performance.html 性能优化 Android应用程序运行的移动设备受限于其运 ...
- 【BZOJ5416】【NOI2018】冒泡排序(动态规划)
[BZOJ5416][NOI2018]冒泡排序(动态规划) 题面 BZOJ 洛谷 UOJ 题解 考场推出了就是两个上升子序列,并且最长下降子序列长度不超过\(2\)...然后大力暴力状压\(dp\)混 ...
- 原生JS节点操作
获取子节点 1. children 不是标准的dom属性,但是几乎被所有浏览器支持.获取子元素的元素节点(只包括元素节点) 注意:在IE中,children包含注释节点. 2. childNodes ...
- CANOE入门(二)
CAPL就是Communication Application Programming Laguage的缩写,CAPL类似于C语言的语法,因此所有的语法请参考C语言教程,这里不在这里进行详述,关于C语 ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- CodeForces - 589B(暴力)
题目链接:http://codeforces.com/problemset/problem/589/B 题目大意:告诉你n 个矩形,知道矩形的长度和宽度(长和宽可以互换),每个矩形的长度可以剪掉一部分 ...
- ACM-ICPC 2015 ChangChun
比赛链接 :点击这里 大概会写 F G J L 吧 F 给你一个序列 最多删除一个数使他构成 最长不上升或者不下降子序列 这题不会不会on的算法只能 t*n*logn 了 还是压常过 求两次 LIS ...
- Spring注解方式实现任务调度
原文:http://docs.spring.io/spring/docs/4.0.1.BUILD-SNAPSHOT/javadoc-api/ 注解类型:EnableScheduling @Target ...
- agc031
T1 题意:给你一个串,求所有子序列个数,满足没有相同字符.1e5,2s. 解:考虑一个合法的子序列.其中每个字母的出现位置都有(出现次数)种选择.还可以不选,要 + 1. 然后乘起来就做完了.如果变 ...
- echart折线区域图
在引入echart区域折线图时,没有出现对应的区域图 当发现引入下面代码到自己的代码中并没有对应的区域图 option = { xAxis: { type: 'category', boundaryG ...