Python 线程(七):local(线程局部存储)
线程局部存储(tls),对于同一个local,线程无法访问其他线程设置的属性;线程设置的属性不会被其他线程设置的同名属性替换。
代码:
1 import threading 2 3 local = threading.local() 4 local.tname = "main" 5 6 def func(info): 7 local.tname = info 8 print local.tname 9 10 t1 = threading.Thread(target=func, args=['funcA']) 11 t2 = threading.Thread(target=func, args=['funcB']) 12 13 t1.start() 14 t1.join() 15 16 t2.start() 17 t2.join() 18 19 print local.tname