Python在map中执行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)的输出看起来很好。

我怀疑最后一行的语法出现错误,如下所示:在“pool.map(call,commands)File”/ home / ect / anaconda2 / lib / python2文件“run_parallel.py”中,第47行。 7 / multiprocessing / pool.py“,第251行,在map中返回self.map_async(func,iterable,chunksize).get()文件”/home/ect/anaconda2/lib/python2.7/multiprocessing/pool.py“第567行,获得self._value

谢谢


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

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

链接地址: http://www.djcxy.com/p/55291.html

上一篇: Python execute subprocess.call inside map

下一篇: Creating network with networkx / plotly from a pandas dataframe