最近在服务器上应用Pharmdock软件进行运算时,只能利用一个cpu,考虑到cpu浪费太多,于是考虑到了多线程
import os
from multiprocessing import Pool
#遍历文件夹,返回分子路径
def get_mol_paths(folder):
return (os.path.join(folder,f) for f in os.listdir(folder) if f.endswith('_omega2.mol2'))
'''
#遍历分子列表,返回分子路径
def get_mol_paths(molslist):
f= open(molslist)
g=[]
for m in f.readlines():
m=m.strip('\n') #去掉每行结尾处的换行符
g.append(m)
return g
'''
def run_cmd(filename):
dock='/usr/local/cloud/PharmDock/bin/docking \
-c /usr/local/cloud/PharmDock/dat/CamDoc.bcf \
-l %s \
-p /map/substr/test/ProtPharm/Docking_Pharmacophore.mol2 \
-ps /map/substr/test/ProtPharm/Scoring_Pharmacophore.mol2 \
-prot /map/substr/test/pro.pdb \
-nmode 1 -o out -limoc' % filename
os.system(dock)
if __name__=='__main__':
folder="/map/free/free_ph"
mols = get_mol_paths(folder)
pool = Pool()
pool.map(run_cmd,mols)
pool.close()
pool.join()
map函数读取mols列表中的文件作为参数分别传入run_cmd进行运算,并行处理后16个cpu可以全部利用,速度大大提升
关于map函数的解析和应用可参照:
http://my.oschina.net/zyzzy/blog/115096
http://www.jb51.net/article/63368.htm