class Widgt(object):
pass
import threading
def test():
local_data = threading.local()
# local_data = Widgt()
local_data.x = 1
def thread_func():
print('Has x in new thread: %s' % hasattr(local_data, 'x'))
local_data.x = 2
t = threading.Thread(target = thread_func)
t.start()
t.join()
print('x in pre thread is %s' % local_data.x)
if __name__ == '__main__':
test()
其值在线程之间是独立的