新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonthread.local()的實例化
1、說明

我們提供的服務(wù)有:成都網(wǎng)站制作、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、大英ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的大英網(wǎng)站制作公司
threading.local()實例化全局對象,該全局對象有大字典,鍵值為兩個弱引用對象{線程對象、字典對象},通過current_thread()獲取當前線程對象,并根據(jù)該對象獲取相應(yīng)的字典對象。
2、實例
import threading
import random
data = threading.local()
def show(d):
try:
num = d.num
except AttributeError:
print("線程 %s 還未設(shè)置該屬性!" % 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學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:pythonthread.local()的實例化
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/djjepcd.html


咨詢
建站咨詢
