Discuss / Python / 很复杂的那个全局字典,因为教学写得不完整,我一开始没看懂,所以写了一个完整的代码,希望给没看懂的人提供参考

很复杂的那个全局字典,因为教学写得不完整,我一开始没看懂,所以写了一个完整的代码,希望给没看懂的人提供参考

Topic source

浴火化影

#1 Created at ... [Delete] [Delete and Lock User]
import threading
global_dict1={}
global_dict2={}

def get(name,act):
   global_dict1[threading.current_thread()]=name
   global_dict2[threading.current_thread()]=act
   action()

def action():
   print(global_dict1[threading.current_thread()] ,end='')
   print(' is ',end='')
   print(global_dict2[threading.current_thread()])

th1=threading.Thread(target=get,args=('Jone','batting'),name='Th_Jone')
th2=threading.Thread(target=get,args=('Dannis','kicking'),name='Th_Dannis')
th1.start()
th2.start()
th1.join()
th2.join()

  • 1

Reply