[英]How to use Python3 Multiprocessing to run Multiple Bash commands
I created a bash script to automate multiple tools.我创建了一个 bash 脚本来自动化多个工具。 This bash script takes a string as input and perform multiple tasks on it.
此 bash 脚本将字符串作为输入并在其上执行多项任务。 Structure of scrips is like
票据的结构就像
#!/bin/bash
tool1 $1
tool2 $1
tool3 $1
tool4 $1
tool5 $1
I want to use Python3 Multiprocessing to run the n-number of tools concurrent/parallel to speedup the Process.我想使用 Python3 多处理来并行/并行运行 n 个工具来加速进程。 How it can be done in Python?
如何在 Python 中完成?
You could use multiprocessing.pool.Pool
along with os.system
like this:您可以像这样使用
multiprocessing.pool.Pool
和os.system
:
import sys
import os
import multiprocessing
tools = ['tool1', 'tool2', 'tool3', 'tool4', 'tool5']
arg1 = sys.argv[1]
p = multiprocessing.Pool(len(tools))
p.map(os.system, (t + ' ' + arg1 for t in tools))
This will start len(tools)
parallel processes which will execute os.system('toolN arg')
.这将启动
len(tools)
并行进程,这些进程将执行os.system('toolN arg')
。
In general though, you don't want Pool(len(tools))
, since it does not scale well if you launch more processes than the number N of cores you have available on your machine, so you should do Pool()
instead.不过,一般来说,您不想要
Pool(len(tools))
,因为如果您启动的进程多于机器上可用的内核数 N ,它的扩展性就不好,因此您应该使用Pool()
代替。 This will still execute each tool but it will do at most N concurrently.这仍将执行每个工具,但最多同时执行 N 个。
Please use multiprocessing
and subprocess
.请使用
multiprocessing
和subprocess
。 When using a custom shell script, if it is not in the PATH then use the full path to the script.使用自定义 shell 脚本时,如果它不在 PATH 中,则使用脚本的完整路径。 If your script is in the same folder as the python script, please use
./script.sh
as your command.如果您的脚本与 python 脚本位于同一文件夹中,请使用
./script.sh
作为您的命令。
Also ensure that there is exec permission for the script that you are running还要确保您正在运行的脚本具有 exec 权限
from multiprocessing import Pool
import subprocess
def run_script(input):
(command,arg_str)=input
print("Starting command :{} with argument {}".format(command, arg_str))
result = subprocess.call(command+" "+arg_str, shell=True)
print("Completed command :{} with argument {}".format(command, arg_str))
return result
with Pool(5) as p: # choose appropriate level of parallelism
# choose appropriate command and argument, can be fetched from sys.argv if needed
exit_codes = p.map(run_script, [('echo','hello1'), ('echo','hello2')])
print("Exit codes : {}".format(exit_codes))
You could use the exit codes to verify the completion status.您可以使用退出代码来验证完成状态。 Sample output:
样品 output:
Starting command :echo with argument hello1
Starting command :echo with argument hello2
hello1
hello2
Completed command :echo with argument hello1
Completed command :echo with argument hello2
Exit codes : [0, 0]
Another way to do this (without python) would be to use GNU Parallel.另一种方法(不使用 python)是使用 GNU Parallel。 The below command does the same thing that the above python script does.
下面的命令与上面的 python 脚本的作用相同。
parallel -k echo ::: 'hello1' 'hello2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.