Python中的并行处理:选项和替代方案

时间:2018-03-27 20:23:26

标签: python parallel-processing multiprocess

我尝试了joblib,但是,我按照here解释设置了处理器关联(我的脚本和错误一起显示在下面)。

现在我想知道是否有其他选项或替代方案可以让我实现相同的目标,即使用我的8个核心(以类似于GNU并行的方式)并行运行相同的脚本。

错误:

AttributeError: 'Process' object has no attribute 'set_cpu_affinity'

我的剧本:

from datetime import datetime
from subprocess import call 
from joblib import Parallel, delayed
import multiprocessing
import psutil
import os

startTime = datetime.now()

pdb_name_list = []
for filename in os.listdir('/home/labusr/Documents/python_scripts/Spyder/Refinement'):
   if filename.endswith(".pdb"):     
       pdb_name_list.append(filename)

num_cores = multiprocessing.cpu_count()
p = psutil.Process(os.getpid())
p.set_cpu_affinity(range(num_cores))
print(p.get_cpu_affinity())

inputs = range(2) 

def strcuture_refine(file_name,i):
    print('Refining strcuture %s round %s......\n' %(file_name, i))
    call(['/home/labusr/rosetta/main/source/bin/rosetta_scripts.linuxgccrelease',
          '-in::file::s', '/home/labusr/Documents/python_scripts/Spyder/_Refinement/%s' %file_name,
          '-parser::protocol', '/home/labusr/Documents/A_asymm_refine.xml',
          '-parser::script_vars', 'denswt=35', 'rms=1.5', 'reso=4.3', 'map=/home/labusr/Documents/tubulin_exercise/masked_map_center.mrc', 
          'testmap=/home/labusr/Documents/tubulin_exercise/mmasked_map_top_centered_resampled.mrc',
          '-in:ignore_unrecognized_res',
          '-edensity::mapreso', '4.3',
          '-default_max_cycles', '200',
          '-edensity::cryoem_scatterers',
          '-beta',
          '-out::suffix', '_%s' %i,
          '-crystal_refine'])
    print('Time for refining %s round %s is: \n' %(file_name, i), datetime.now() - startTime)

for file_name in pdb_name_list:
    Parallel(n_jobs=num_cores)(delayed(strcuture_refine)(file_name,i) for i in inputs)

1 个答案:

答案 0 :(得分:0)

最简单的方法是从例如启动多个Python进程命令行。要使每个进程处理自己的文件,可以在调用Python时传递它:

filename

然后通过

在Python中提供传递的import sys filename = sys.argv[1]
{{1}}