本文首发于知乎
考虑下面一种情形
- 有一些人的信息
person_list = [{'name': 'Bob', 'score': 5}, {'name': 'Mary', 'score': 4}]
- 用两个线程分别更新两个人的
score
,第一次加1,第二次平方,再设定一个count
变量表示更新的次数
我们可以看出上面提到的score
和count
都有全局变量的感觉,但是又不完全是,因为他们只能属于一个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编程
专栏目录:目录
版本说明:软件及包版本说明