如何在 python 中并行化 运行 许多可执行文件的“**for-loop**”?
How to parallelize the "**for-loop**" that run many executbles in order in python?
我有一个 python 脚本可以读取许多用 C 程序编写和编译的可执行文件。这些可执行文件没有问题。但是,当我必须在 for 循环中 运行 这些可执行文件时,我试图并行化循环。
Note: prog1,prog2,prog3 must run in order.
This is a sample example, but in my real code
prog2 depends on output of prog1, and prog3
depends on output of prog2 and so on.
I have seven executables in for loop of iteration 20,
it takes more than 2 hour to complete the process.
If i could parallize the code, it would save a lot of time.
Help would be greatly appreciated!!!!
在我的代码示例 1 中 运行 没问题,但示例 2 没有 运行。
完整代码如下:
#!/usr/bin/python
from multiprocessing import Pool
import os, sys, subprocess, math, re, shutil,copy
#function to run a program and write output to the shell
################################################################################
def run_process(name, args,):
print "--------------------------------------------------------------------"
print "Running: %s"%name
print "Command:"
for arg in args:
print arg,
print ""
print "--------------------------------------------------------------------"
process = subprocess.Popen(args)
process.communicate()
if process.returncode != 0:
print "Error: %s did not terminate correctly. Return code: %i."%(name, process.returncode)
sys.exit(1) # this will exit the code in case of error
###########################
# example 1
#run_process("prog1.c", ['./prog1'])
#run_process("prog2.c", ['./prog2'])
#run_process("prog3.c", ['./prog3', 'first argument'])
# example 2 (parallizing)
commands = []
for x in range(0,20):
commands.extend(("prog1.c",['./prog1']))
commands.extend(("prog2.c",['./prog2']))
commands.extend(("prog3.c",['./prog3', 'first argument']))
p = Pool()
p.map(run_process, commands)
在这里,如果我 运行 示例 1 它 运行 是完美的。但是当我尝试 运行 示例 2 时,它给出了以下错误:
TypeError: run_process() takes exactly 2 arguments (1 given)
进一步说明:
为了创建可执行文件 prog1、prog2 和 prog3,我编写了 C 代码。
看起来像这样:
// to compile: gcc -o prog1 prog1.c
// to run : ./prog1
#include <stdio.h>
int main() {
printf("This is program 1\n");
return 0; }
prog2 看起来完全一样。 prog3 看起来像这样:
//to compile: gcc -o prog3 prog3.c
//to run: ./prog3 'argument1'
#include <stdio.h>
int main(int argc, char ** argv) {
printf("This is program 3\n");
printf("The argument is = %s\n", argv[1]);
return 0; }
现在,for 循环中有 21 次迭代。
在第一次迭代中,它假设它 运行s 可执行文件 prog1,prog2....,prog7
最后产生 ouptput1.fits.
在第二次交互中,它再次按顺序 运行 七个可执行文件并生成 output2.fits.
最后它创建了 21 个适合的文件。
我能做的是做四个函数:
func1 for 循环 0 到 5
fucn2 for 循环 5 到 10
func3 for loop 11 to 15
func4 for loop 16 to 21
然后我想运行这四个函数并行处理。
我的问题是:如何 运行 示例 2 没有任何错误?
import multiprocessing
for x in range(0,20):
multiprocessing.Process(target=run_process, args=("colour.c",['./cl',"color.txt",str(x) ])
...
不太确定我还能添加什么...
Python 有 Pool 个专门为此目的构建的进程。
考虑到您需要 运行 X 次相同的命令序列,并且假设命令序列可以 运行 并行。这意味着第 N 个 运行 可以 运行 和第 N+1 个没有任何暗示。
from multiprocessing import Pool
commands = tuple(("prog1.c",['./prog1']), ...)
def run_processes(execution_index):
print("Running sequence for the %d time." % execution_index)
for command in commands:
process = subprocess.Popen(command)
...
p = Pool()
p.map(run_processes, range(20))
在 Pyhton3 上你可以使用 ProcessExecutor.
每当你想运行并发执行某些事情时,你需要先了解执行边界。如果两条执行线相互依赖,您要么在两者之间建立通信(例如使用管道),要么避免 运行 同时连接它们。
在您的情况下,这些命令是相互依赖的,因此同时 运行 它们会出现问题。但是,如果整个序列不是相互依赖的,那么你可以 运行 那些并行的。
看看 group functions of Celery's canvas do. They allow you to call functions at the same time, with different set of arguments. Say you want to process a total of 1000 elements in your for loop. Doing the same sequentially is highly unoptimized. A simple solution will be to call the same function with two sets of arguments. Even this simple hack will bring down your processing time down by half. That is what Canvas 和 Celery 是什么。
我有一个 python 脚本可以读取许多用 C 程序编写和编译的可执行文件。这些可执行文件没有问题。但是,当我必须在 for 循环中 运行 这些可执行文件时,我试图并行化循环。
Note: prog1,prog2,prog3 must run in order.
This is a sample example, but in my real code
prog2 depends on output of prog1, and prog3
depends on output of prog2 and so on.
I have seven executables in for loop of iteration 20,
it takes more than 2 hour to complete the process.
If i could parallize the code, it would save a lot of time.
Help would be greatly appreciated!!!!
在我的代码示例 1 中 运行 没问题,但示例 2 没有 运行。 完整代码如下:
#!/usr/bin/python
from multiprocessing import Pool
import os, sys, subprocess, math, re, shutil,copy
#function to run a program and write output to the shell
################################################################################
def run_process(name, args,):
print "--------------------------------------------------------------------"
print "Running: %s"%name
print "Command:"
for arg in args:
print arg,
print ""
print "--------------------------------------------------------------------"
process = subprocess.Popen(args)
process.communicate()
if process.returncode != 0:
print "Error: %s did not terminate correctly. Return code: %i."%(name, process.returncode)
sys.exit(1) # this will exit the code in case of error
###########################
# example 1
#run_process("prog1.c", ['./prog1'])
#run_process("prog2.c", ['./prog2'])
#run_process("prog3.c", ['./prog3', 'first argument'])
# example 2 (parallizing)
commands = []
for x in range(0,20):
commands.extend(("prog1.c",['./prog1']))
commands.extend(("prog2.c",['./prog2']))
commands.extend(("prog3.c",['./prog3', 'first argument']))
p = Pool()
p.map(run_process, commands)
在这里,如果我 运行 示例 1 它 运行 是完美的。但是当我尝试 运行 示例 2 时,它给出了以下错误:
TypeError: run_process() takes exactly 2 arguments (1 given)
进一步说明:
为了创建可执行文件 prog1、prog2 和 prog3,我编写了 C 代码。
看起来像这样:
// to compile: gcc -o prog1 prog1.c
// to run : ./prog1
#include <stdio.h>
int main() {
printf("This is program 1\n");
return 0; }
prog2 看起来完全一样。 prog3 看起来像这样:
//to compile: gcc -o prog3 prog3.c
//to run: ./prog3 'argument1'
#include <stdio.h>
int main(int argc, char ** argv) {
printf("This is program 3\n");
printf("The argument is = %s\n", argv[1]);
return 0; }
现在,for 循环中有 21 次迭代。
在第一次迭代中,它假设它 运行s 可执行文件 prog1,prog2....,prog7
最后产生 ouptput1.fits.
在第二次交互中,它再次按顺序 运行 七个可执行文件并生成 output2.fits.
最后它创建了 21 个适合的文件。
我能做的是做四个函数:
func1 for 循环 0 到 5
fucn2 for 循环 5 到 10
func3 for loop 11 to 15
func4 for loop 16 to 21
然后我想运行这四个函数并行处理。
我的问题是:如何 运行 示例 2 没有任何错误?
import multiprocessing
for x in range(0,20):
multiprocessing.Process(target=run_process, args=("colour.c",['./cl',"color.txt",str(x) ])
...
不太确定我还能添加什么...
Python 有 Pool 个专门为此目的构建的进程。
考虑到您需要 运行 X 次相同的命令序列,并且假设命令序列可以 运行 并行。这意味着第 N 个 运行 可以 运行 和第 N+1 个没有任何暗示。
from multiprocessing import Pool
commands = tuple(("prog1.c",['./prog1']), ...)
def run_processes(execution_index):
print("Running sequence for the %d time." % execution_index)
for command in commands:
process = subprocess.Popen(command)
...
p = Pool()
p.map(run_processes, range(20))
在 Pyhton3 上你可以使用 ProcessExecutor.
每当你想运行并发执行某些事情时,你需要先了解执行边界。如果两条执行线相互依赖,您要么在两者之间建立通信(例如使用管道),要么避免 运行 同时连接它们。
在您的情况下,这些命令是相互依赖的,因此同时 运行 它们会出现问题。但是,如果整个序列不是相互依赖的,那么你可以 运行 那些并行的。
看看 group functions of Celery's canvas do. They allow you to call functions at the same time, with different set of arguments. Say you want to process a total of 1000 elements in your for loop. Doing the same sequentially is highly unoptimized. A simple solution will be to call the same function with two sets of arguments. Even this simple hack will bring down your processing time down by half. That is what Canvas 和 Celery 是什么。