threading.local的作用?

为每个线程创建一个独立的空间,使得线程对自己的空间中的数据进行操作(数据隔离)。

 

import threading
from threading import local
import time
 
obj = local()
 
 
def task(i):
    obj.xxxxx = i
    time.sleep(2)
    print(obj.xxxxx,i)
 
for i in range(10):  #开启了10个线程
    t = threading.Thread(target=task,args=(i,))
    t.start()
posted @ 2019-12-18 08:52  Mr_Riven  阅读(219)  评论(0编辑  收藏  举报