转:Python: threading.local是全局变量但是它的值却在当前调用它的线程当中
原文地址:http://www.cnblogs.com/fengmk2/archive/2008/06/04/1213958.html
在threading module中,有一個(gè)非常特別的類local。一旦在主線程實(shí)例化了一個(gè)local,它會(huì)一直活在主線程中,并且又主線程啟動(dòng)的子線程調(diào)用這個(gè)local實(shí)例時(shí),它的值將會(huì)保存在相應(yīng)的子線程的字典中。
我們先看看測(cè)試代碼:
#!/usr/bin/python# -*- coding: utf-8 -*-
# Description: test the threading.local class
# Create: 2008-6-4
# Author: MK2[fengmk2@gmail.com]
from threading import local, enumerate, Thread, currentThread
local_data = local()
local_data.name = 'local_data'
class TestThread(Thread):
def run(self):
print currentThread()
print local_data.__dict__
local_data.name = self.getName()
local_data.add_by_sub_thread = self.getName()
print local_data.__dict__
if __name__ == '__main__':
print currentThread()
print local_data.__dict__
t1 = TestThread()
t1.start()
t1.join()
t2 = TestThread()
t2.start()
t2.join()
print currentThread()
print local_data.__dict__
運(yùn)行結(jié)果:
<_MainThread(MainThread, started)>
{'name': 'local_data'}
<TestThread(Thread-1, started)>
{}
{'add_by_sub_thread': 'Thread-1', 'name': 'Thread-1'}
<TestThread(Thread-2, started)>
{}
{'add_by_sub_thread': 'Thread-2', 'name': 'Thread-2'}
<_MainThread(MainThread, started)>
{'name': 'local_data'}
?
主線程中的local_data并沒有被改變,而子線程中的local_data各自都不相同。
怎么這么神奇?local_data具有全局訪問權(quán),主線程,子線程都能訪問它,但是它的值卻是各當(dāng)前線程有關(guān),究竟什么奧秘在這里呢?
查看了一下local的源代碼,發(fā)現(xiàn)就神奇在_path()方法中:
def _patch(self):key = object.__getattribute__(self, '_local__key')
d = currentThread().__dict__.get(key)
if d is None:
d = {}
currentThread().__dict__[key] = d
object.__setattr__(self, '__dict__', d)
# we have a new instance dict, so call out __init__ if we have
# one
cls = type(self)
if cls.__init__ is not object.__init__:
args, kw = object.__getattribute__(self, '_local__args')
cls.__init__(self, *args, **kw)
else:
object.__setattr__(self, '__dict__', d)?
每次調(diào)用local實(shí)例的屬性前,local都會(huì)調(diào)用這個(gè)方法,找到它保存值的地方.
d = currentThread().__dict__.get(key)? 就是這個(gè)地方,確定了local_data值的保存位置。所以子線程訪問local_data時(shí),并不是獲取主線程的local_data的值,在子線程 第一次訪問它是,它是一個(gè)空白的字典對(duì)象,所以local_data.__dict__為 {},就像我們的輸出結(jié)果一樣。
如果想在當(dāng)前線程保存一個(gè)全局值,并且各自線程互不干擾,使用local類吧。
轉(zhuǎn)載于:https://www.cnblogs.com/Donal/archive/2011/03/09/1979222.html
總結(jié)
以上是生活随笔為你收集整理的转:Python: threading.local是全局变量但是它的值却在当前调用它的线程当中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [恢]hdu 1019
- 下一篇: 图片有花