简体   繁体   English

如何在python中并行运行多个进程

[英]how to run multiple process in parallel in python

the heading is very generic but issue might not be. 标题非常通用,但问题可能并非如此。

I have a script that is compiling some code with the parameters passed from a file(xls file). 我有一个脚本,正在使用从文件(xls文件)传递的参数来编译一些代码。 Based on number of configurations on xls i have to compile certain files. 根据xls上的配置数量,我必须编译某些文件。 I want to store result of each compilation(stdout and stderr) in text files whose names comes from configuration. 我想将每个编译(stdout和stderr)的结果存储在名称来自配置的文本文件中。

I have been able to do all this but to speed up things i want to run all the compilation in parallel. 我已经能够做到所有这些,但是为了加快速度,我想并行运行所有编译。 Is there a way to do this? 有没有办法做到这一点?

Sample file.. 样本文件

for n in num_rows: # num_rows store all the rows read using xlrd object
    parameters_list = [...] # has all the parameters read from xls
    .
    .
    .
    logfile = ...txt #name is based on name read from xls

    p = subprocess.Popen(parameters_list, stderr=logfile)
    p.wait()
    logfile.close()

I have to wait for each process to be over before closing the file. 我必须等待每个进程结束才能关闭文件。

My problem might be too long but any help or leads are welcomed. 我的问题可能太长了,但是欢迎任何帮助或线索。

You can do this using a multiprocessing.Pool: 您可以使用multiprocessing.Pool来做到这一点:

def parse_row(n):
    parameters_list = [...] # has all the parameters read from xls
    .
    .
    .
    logfile = ...txt #name is based on name read from xls
    p = subprocess.Popen(parameters_list, stderr=logfile)
    p.wait()
    logfile.close()
pool = multiprocessing.Pool()
pool.map_async(parse_row, num_rows)
pool.close()
pool.join()

Assuming your processes will all be writing to different logfiles, the answer is quite simple: the subprocess module will already run things in parallel. 假设所有进程都将写入不同的日志文件,答案很简单: subprocess进程模块已经可以并行运行事物。 Just create a different Popen object for each one, and store them in a list: 只需为每个对象创建一个不同的Popen对象,并将它们存储在列表中:

processes = []
logfiles = []
for n in num_rows: # num_rows store all the rows read using xlrd object
    parameters_list = [...] # has all the parameters read from xls
    .
    .
    .
    logfile = ...txt #name is based on name read from xls
    logfiles.append(logfile)

    p = subprocess.Popen(parameters_list, stderr=logfile)
    logfiles.append(logfile)
    processes.append(p)

# Now, outside the for loop, the processes are all running in parallel.
# Now we can just wait for each of them to finish, and close its corresponding logfile

for p, logfile in zip(processes, logfiles):
    p.wait() # This will return instantly if that process was already finished
    logfile.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM