美文网首页
ThreadLocal

ThreadLocal

作者: 轻狂清风 | 来源:发表于2018-09-27 21:41 被阅读0次

    import threading

    local_school=threading.local()

    def process_student():

        #获取当前线程关联的student:

        std=local_school.student

        print('Hello,%s(in %s)' % (std,threading.current_thread().name))

    def process_thread(name):

        #绑定ThreadLocal的student:

        local_school.student=name

        process_student()

    t1=threading.Thread(target=process_thread,args=('Alice',),name='Thread-A')

    t2=threading.Thread(target=process_thread,args=('Bob',),name='Thread-B')

    t1.start()

    t2.start()

    t1.join()

    t2.join()

    相关文章

      网友评论

          本文标题:ThreadLocal

          本文链接:https://www.haomeiwen.com/subject/esmyoftx.html