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

 

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()

相关文章:

  • 2021-06-05
  • 2021-09-16
  • 2022-02-02
  • 2021-08-28
  • 2021-10-14
  • 2021-12-30
  • 2021-07-16
  • 2021-09-05
猜你喜欢
  • 2021-09-30
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
相关资源
相似解决方案