import time import threading from threading import local class Foo(local): pass # 实例化(创建)对象 foo = Foo() # 给Foo类添加静态变量 def add(i): foo.num = i time.sleep(1) # threading.current_thread().ident 线程PID print(foo.num, i, threading.current_thread().ident) for i in range(20): # 开启多线程 th = threading.Thread(target=add, args=(i,)) th.start()