python多線程threadpool傳入多個參數並獲取方法返回值

python多線程

通過創建線程池threadpool的方法,向方法中傳入多個參數,同時獲取方法的返回值。threadpool是第三方模塊,需要先進行安裝,
pip instll threadpool

import threadpool
#多線程執行的方法
def retry_read_ApsStatus(pairDate,pairShift,site,flag):
#flag返回的值爲1或0
#方法省略	
        return flag
#回調函數,接收的參數(請求本身,和請求工作函數執行結果        
def get_result(request,result):
    global results
    results.append(result)

results = []
# 聲明可容納五個線程的池
pool = threadpool.ThreadPool(3)
# 創建線程運行內容請求列表(線程工作函數,線程工作參數列表,回調函數)
list_var1 = [parse_date, parse_shift, 2080, ne_flag]
list_var3 = [parse_date, parse_shift, 2090, sz_flag]
list_var2 = [parse_date, parse_shift, 5060, xb_flag]
par_list = [(list_var1, None), (list_var2, None), (list_var3, None)]
re = threadpool.makeRequests(retry_read_ApsStatus, par_list, get_result)

#將每一個線程放到線程池
res = [pool.putRequest(req) for req in re]
pool.wait()
print("多線程返回結果")
print(results)
#多線程返回結果
#[1, 1, 1]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章