RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
创新互联Python教程:pythonthread.local()的实例化

1、说明

我们提供的服务有:成都网站制作、做网站、微信公众号开发、网站优化、网站认证、大英ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的大英网站制作公司

threading.local()实例化全局对象,该全局对象有大字典,键值为两个弱引用对象{线程对象、字典对象},通过current_thread()获取当前线程对象,并根据该对象获取相应的字典对象。

2、实例

import threading
import random
 
data = threading.local()
def show(d):
try:
        num = d.num    
except AttributeError:        
print("线程 %s 还未设置该属性!" % threading.current_thread().getName())    
else:
  print("线程 %s 中该属性的值为 = %s" % (threading.current_thread().getName(), num))
def thread_call(d):    
show(d)    
d.num = random.randint(1, 100)    
show(d)
if __name__ == '__main__':    
show(data)    
data.num = 666   
 show(data)    
for i in range(2):        
t = threading.Thread(target=thread_call, args=(data,), name="Thread " + str(i))        t.start()

以上就是python thread.local()()的实例化,希望对大家有所帮助。更多Python学习指路:创新互联python教程


本文名称:创新互联Python教程:pythonthread.local()的实例化
URL分享:http://www.jxjierui.cn/article/djjepcd.html