2016-09-08 36 views
0

您好我有以下代码:的Python执行内部地图subprocess.call

import sys 
import multiprocessing as mp 


def create_parser(): 
    from argparse import ArgumentParser, FileType 
    ap = ArgumentParser() 
    ap.add_argument('infile', type=FileType('r'), 
     help="file with shell commands to execute") 
    ap.add_argument('-n', '--ncpu', type=int, default=0, 
     help="Number of CPUs to use (default: %(default)s: all CPUs)") 
    return ap 


def cpus_to_use(ncpu): 
    return ncpu if ncpu else mp.cpu_count() 


if __name__ == '__main__': 
    from subprocess import call 

    ap = create_parser() 
    args = ap.parse_args(sys.argv[1:]) 

    ncpu = cpus_to_use(args.ncpu) 

    if args.infile: 
     # Read commands from already open file and close 
     commands = [c for c in args.infile.read().split('\n') if c] 
     args.infile.close() 

     # Create a pool and map run_cmd to the shell commands 
     pool = mp.Pool(processes=ncpu) 
     pool.map(call, commands) 

我基本上导入从具有每行一个特定的命令来执行(即我在命令行的文本文件试图并行化)。我正在使用Python 2.7.12,并且print(command)的输出看起来很好。

我怀疑有在最后一行行语法错误当我越来越: 文件“run_parallel.py”,第47行,在 pool.map(打电话,命令) 文件“/家/ ECT /anaconda2/lib/python2.7/multiprocessing/pool.py“,第251行,在地图 返回self.map_async(func,iterable,chunksize).get() 文件”/ home/ect/anaconda2/lib/python2 0.7 /多/ pool.py”,线路567,在获取 提高self._value

谢谢

回答

0
commands = [c.split() for c in args.infile.read().split('\n') if c] 

(这是从http://sburns.org/2014/05/03/cortical-tractography-recipe.html,对吧?我只是有同样的问题:) 它似乎通过拆分命令中的每个条目工作在空白处,以便池能够正确地解析每个函数调用的参数。

+0

非常感谢您的回答! – Francesco

+0

这是一个有点偏离主题,但我没有设法运行在'sburns'的'教程'中运行的纤维束...不知怎的,probtrackx似乎并没有去任何地方 - 所以,如果你发现任何其他错误代码/脚本在网站上,请让我知道,我也会这样做! – mrburnst

相关问题