python回調(diào)函數(shù)中使用多線程的方法
下面的demo是根據(jù)需求寫的簡(jiǎn)單測(cè)試腳本
#!/usr/bin/env python
# coding: utf-8
# 第一個(gè)列表為依賴組件和版本號(hào),后面緊跟負(fù)責(zé)人名稱
# 接著出現(xiàn)第二個(gè)以來組建列表,負(fù)責(zé)人為空了
# 所以根據(jù)需求需要對(duì)組件、版本號(hào)、負(fù)責(zé)人進(jìn)行不同處理
# 這時(shí)在for循環(huán)中根據(jù)if判斷,寫回調(diào)函數(shù)處理
# 格式不一致數(shù)據(jù)的測(cè)試數(shù)據(jù)
a = [[u'tool-1', u'1.9.13'], u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23'], [u'tool-3', u'1.9.33'], [u'tool-4', u'1.9.43'], u'pi',[u'tool-5', u'1.9.53']]
# a = [[u'tool-1', u'1.9.13'],u'xiaowang',[u'tool-2', u'1.9.23'],u'xiaowang', [u'tool-3', u'1.9.33'],u'xiaowang']
# a = [[u'tool-1', u'1.9.13']]
# [u'tool-1', u'1.9.13']
your_pro = a[0]
# print your_pro
# [u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23']]
tmp = a[1:]
# print tmp
def git_callback(whole_v, proj_value, name_value):
# 如果存在負(fù)責(zé)人存在
try:
if type(name_value[0]) is unicode:
# 對(duì)除去列表0個(gè)索引的數(shù)據(jù)(依賴名和版本號(hào))后面的數(shù)據(jù)進(jìn)行遍歷
for i in name_value:
# 碰到后面的數(shù)據(jù)是列表的進(jìn)行回調(diào)
if type(i) is list:
tmp_index = whole_v.index(i)+1
return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:])
else:
# 打印依賴、版本號(hào) 負(fù)責(zé)人 開始
print proj_value+i.split()+['start']
else:
# 如果負(fù)責(zé)人后跟的組件這種格式的列表數(shù)據(jù)為空
# 也就是只有依賴和版本號(hào)列表數(shù)據(jù),負(fù)責(zé)人為空,就打印依賴版本號(hào)
ver = proj_value
owner = name_value
if type(owner[0]) is unicode:
return git_callback(whole_v, ver, owner)
else:
print ver
# 這里是為了判斷是不是到列表的最后一位
# 如果是最后一個(gè)值,且不是字符串的Unicode,而是列表
# 就直接打印出項(xiàng)目
if whole_v.index(owner[0]) == len(whole_v)-1:
# 打印最后一個(gè)值
print whole_v[-1:]
else:
# 這里比較繞,打印調(diào)試吧...
new_ver = whole_v[whole_v.index(ver)+1]
owner = whole_v[whole_v.index(ver)+2:]
return git_callback(whole_v, new_ver, owner)
except IndexError as e:
print proj_value
print e
git_callback(a, your_pro, tmp)
demo的output:
Boom:git_response pirogue$ python test.py [u'tool-1', u'1.9.13', u'xiaowang', 'start'] [u'tool-1', u'1.9.13', u'xiaoqu', 'start'] [u'tool-2', u'1.9.23'] [u'tool-3', u'1.9.33'] [u'tool-4', u'1.9.43', u'pi', 'start'] [u'tool-5', u'1.9.53'] list index out of range
python的多線程
下面的代碼是從主程序中,摘取出來的代碼片段
from multiprocessing.dummy import Pool as ThreadPool
# 判斷git查詢返回的依賴數(shù)據(jù)格式不唯一的回調(diào)
def git_callback(whole_v, proj_value, name_value, git_cookie):
#
whole_v = whole_v
list_git = []
if name_value:
# print name_value
for i in name_value:
# print i
if i:
if type(i) is list:
tmp_index = whole_v.index(i)+1
return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:], git_cookie)
else:
git_cookie = str(git_cookie.split()[0])+' '+str(git_cookie.split()[1])
list_git.append(tuple(git_cookie.split("?")+i.split()))
print list_git
pool = ThreadPool(100)
result = pool.map(pool_git, list_git)
print result
pool.close()
pool.join()
else:
print proj_value
上面的多線程代碼片段是一個(gè)回調(diào)函數(shù),沒有完全根據(jù)demo進(jìn)行改裝,有了demo根據(jù)需求改起來也不難,多調(diào)試就可以了。
python多線程接收多個(gè)參數(shù)
from multiprocessing.dummy import Pool as ThreadPool pool = ThreadPool(100) result = pool.map(pool_git, list_git) print result pool.close() pool.join()
pool_git是你需要多線程調(diào)用的功能函數(shù),list_git是pool_git函數(shù)需要接收的參數(shù),默認(rèn)情況下pool_git是一個(gè)接收一個(gè)參數(shù)的函數(shù)。
但是我們的功能常常設(shè)計(jì)的邏輯比較復(fù)雜,需要在pool_git中傳入多個(gè)參數(shù),這時(shí)list_git就應(yīng)該給一個(gè)多個(gè)元組組成的列表。
stackoverflow上老外給的代碼示例:
def multi_run_wrapper(args): return add(*args) def add(x,y): return x+y if __name__ == "__main__": from multiprocessing import Pool pool = Pool(4) results = pool.map(multi_run_wrapper,[(1,2),(2,3),(3,4)]) print results output [3, 5, 7]
Stack Overflow上更多的答疑方便你更好的理解:
https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments
相信聰明的你一定能看得懂~
多線程與多進(jìn)程
from multiprocessing.dummy import Pool as ThreadPool
多線程進(jìn)程池,綁定一個(gè)CPU核心
from multiprocessing import Pool
多進(jìn)程,運(yùn)行于多個(gè)cpu核心
如果你搞不懂是CPU密集型的任務(wù),還是IO密集型的任務(wù),那就用這個(gè)庫(kù)兩條import都寫上,然后分別實(shí)例化跑一下就知道耗時(shí)長(zhǎng)短,用法上只是在創(chuàng)建對(duì)象上改幾個(gè)字母就行Pool和ThreadPool的互換。
總結(jié)
以上所述是小編給大家介紹的python回調(diào)函數(shù)中使用多線程的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
用python結(jié)合jieba和wordcloud實(shí)現(xiàn)詞云效果
詞云,顧名思義就是很多個(gè)單詞,然后通過出現(xiàn)的頻率或者比重之類的標(biāo)準(zhǔn)匯聚成一個(gè)云朵的樣子嘛,其實(shí)呢現(xiàn)在網(wǎng)上已經(jīng)有很多能自動(dòng)生成詞云的工具了,比如Wordle,Tagxedo等等,Python也能實(shí)現(xiàn)這樣的效果,我們通過jieba庫(kù)和wordcloud庫(kù)也能十分輕松的完成詞云的構(gòu)建2017-09-09
詳解Python中打亂列表順序random.shuffle()的使用方法
這篇文章主要介紹了詳解Python中打亂列表順序random.shuffle()的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python cookbook(字符串與文本)在字符串的開頭或結(jié)尾處進(jìn)行文本匹配操作
這篇文章主要介紹了Python cookbook(字符串與文本)在字符串的開頭或結(jié)尾處進(jìn)行文本匹配操作,涉及Python使用str.startswith()和str.endswith()方法針對(duì)字符串開始或結(jié)尾處特定文本匹配操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04
Python安裝Numpy和matplotlib的方法(推薦)
下面小編就為大家?guī)硪黄狿ython安裝Numpy和matplotlib的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Pytorch使用卷積神經(jīng)網(wǎng)絡(luò)對(duì)CIFAR10圖片進(jìn)行分類方式
這篇文章主要介紹了Pytorch使用卷積神經(jīng)網(wǎng)絡(luò)對(duì)CIFAR10圖片進(jìn)行分類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
django的autoreload機(jī)制實(shí)現(xiàn)
這篇文章主要介紹了django的autoreload機(jī)制實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

