Discuss / Python / 这样也可以实现呀,跟老师写的那个有什么区别呢?求解

这样也可以实现呀,跟老师写的那个有什么区别呢?求解

Topic source

import threading local_school =threading.local() def process_student(std): local_school.student=std print('hello %s in (%s)\n' % (std,threading.cur rent_thread().name)) t1 =threading.Thread(target =process,args =('qwe',),name='A') t2 =threading.Thread(target =process,args =('asd',),name ='B') t1.start() t2.start() t1.join() t2.join()

_全I

#2 Created at ... [Delete] [Delete and Lock User]

有很大区别啊。

老师的例子是说明,在线程process_thread里的函数process_student里传递变量student啊。这时用ThrreadLocal变量local_school保存了不同线程Thread-A和Thread-B的值。

其实这个例子就是想说明ThreadLocal能够方便地在线程各自的函数传递变量而互不干扰而已。你贴的代码完全没有使用ThreadLocal传递变量啊。


  • 1

Reply