2016-11-12 34 views
0

我正在嘗試使用python多處理模塊來加速一些計算。第一步是從BioModels數據庫中獲取一些模型。這個名爲BioServices的API可以通過pip install bioservices進行下載。我已經設法做到這一點,但這需要時間,並會從並行中受益。使用Python多處理模塊從BioModels數據庫下載模型

bio=bioservices.BioModels() #initialize the class for downloading models 
m=bio.getAllCuratedModelsId() #assign model ID's to the m (a python list) 
def f(ID): 
    dct={} 
    name=bio.getModelNameById(ID)#retrieve the model name for the result dict key 
    print 'running {}'.format(name) #print some information so you can see the program working 
    dct[name]=bio.getModelSBMLById(ID) #get the model and assign as value in dct 
    time.sleep(0.5) #stop the program for a bit to prevent bombarding the services and being cut out 
    return dct 
model_dct={} 
P=multiprocessing.Pool(8) 
for i in m: 
    model_dct.update(P.map(f,i)) # parallelize 

print time.time()-start+'seconds' 

目前這只是初始化生物類和崩潰(或至少無所作爲)。任何人都可以建議如何解決我的代碼?

感謝

回答

0

Pool.map是指在一個迭代函數適用於所有項目,所以你應該說:

for i in m: 
    ...P.map(f,i) 

而只是

P.map(f, m) 

這會給你一個字典列表,每個ID一個。