我正在尝试使用python多处理模块来加速一些计算。第一步是从BioModels数据库中获取大量模型。有一个名为BioServices的接口,可以通过pip install bioservices下载。我已经成功地在串行中做到了这一点,但这需要时间,并将从并行中受益。
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'目前,这只是初始化bio类并崩溃(或者至少什么都不做)。有人能建议如何修复我的代码吗?
谢谢
发布于 2016-11-13 06:48:39
Pool.map的目的是将一个函数应用于可迭代中的所有项,因此您应该这样说:
for i in m:
...P.map(f,i)而不是仅仅
P.map(f, m)这将为您提供一个字典列表,每个ID对应一个字典。
https://stackoverflow.com/questions/40568250
复制相似问题