Threadlocal

本文首发于知乎
考虑下面一种情形

  • 有一些人的信息person_list = [{'name': 'Bob', 'score': 5}, {'name': 'Mary', 'score': 4}]
  • 用两个线程分别更新两个人的score,第一次加1,第二次平方,再设定一个count变量表示更新的次数

我们可以看出上面提到的scorecount都有全局变量的感觉,但是又不完全是,因为他们只能属于一个person而不是整个全局。这种情况本方法可以有两个

  • 一种方法是函数之间构造中间变量进行传递,但是这样代码非常冗余
  • 一种方法是创造全局变量,但是有多个person,如果要全局变量只能创造一个字典,每个人再分别提取

我们来看一下第二种方法

from threading import Thread
global_dict = {'bob': {'name': 'Bob', 'score': 5, 'count': 0},
'mary': {'name': 'Mary', 'score': 4, 'count': 0}}
def work(person):
print('start score', global_dict[person]['score'])
update_first(person)
update_second(person)
print('end count', global_dict[person]['count'])
def update_first(person):
global global_dict
global_dict[person]['score'] = global_dict[person]['score'] + 1
print(global_dict[person]['name'], 'first new score is', global_dict[person]['score'])
global_dict[person]['count'] += 1
def update_second(person):
global global_dict
global_dict[person]['score'] = global_dict[person]['score'] * global_dict[person]['score']
print(global_dict[person]['name'], 'second new score is', global_dict[person]['score'])
global_dict[person]['count'] += 1
Thread(target = work, args = ('bob', )).start()
Thread(target = work, args = ('mary', )).start()
复制代码

运行结果如下

start score 5
Bob first new score is 6
Bob second new score is 36
start score 4
end count 2
Mary first new score is 5
Mary second new score is 25
end count 2
复制代码

一直调用全局变量字典使代码非常冗余,有两种比较好的方法

  • 类的形式
  • ThreadLocal

首先来看一下写成类的形式

from threading import Thread
class MyThread(Thread):
def __init__(self, name, score):
Thread.__init__(self)
self.name = name
self.score = score
self.count = 0
def run(self):
print('start score', self.score)
self.update_first()
self.update_second()
print('end count', self.count)
def update_first(self):
self.score = self.score + 1
print(self.name, 'first new score is', self.score)
self.count += 1
def update_second(self):
self.score = self.score * self.score
print(self.name, 'second new score is', self.score)
self.count += 1
MyThread('Bob', 5).start()
MyThread('Mary', 4).start()
复制代码

输出结果如下

start score 5
Bob first new score is 6
Bob second new score is 36
start score 4
end count 2
Mary first new score is 5
Mary second new score is 25
end count 2
复制代码

我们知道,threading模块中多线程实现有两种方式,上面从类继承是一种,还有一种是将函数传入Thread中。第二种就需要使用threadlocal才能比较好地实现上述过程。代码如下

import threading
local = threading.local()
def work(name, score):
local.name = name
local.score = score
local.count = 0
print('start score', local.score)
update_first()
update_second()
print('end count', local.count)
def update_first():
local.score = local.score + 1
print(local.name, 'first new score is', local.score)
local.count += 1
def update_second():
local.score = local.score * local.score
print(local.name, 'second new score is', local.score)
local.count += 1
threading.Thread(target = work, args = ('Bob', 5)).start()
threading.Thread(target = work, args = ('Mary', 4)).start()
复制代码

运行结果如下

start score 5
Bob first new score is 6
Bob second new score is 36
start score 4
end count 2
Mary first new score is 5
Mary second new score is 25
end count 2
复制代码

所以说threadlocal就是用于处理那种介于全局变量和局部变量之间的变量的。

欢迎关注我的知乎专栏

专栏主页:python编程

专栏目录:目录

版本说明:软件及包版本说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值