简体   繁体   English

如何在python中并行执行多个bash命令

[英]How to execute multiple bash commands in parallel in python

So, I have a code which takes in input and starts a spark job in cluster.. So, something like 因此,我有一个接受输入并在集群中启动Spark工作的代码。

spark-submit driver.py -i input_path

Now, I have list of paths and I want to execute all these simulatenously.. 现在,我有了路径列表,我想模拟所有这些路径。

Here is what I tried 这是我尝试过的

base_command = 'spark-submit driver.py -i %s'
for path in paths:
   command = base_command%path
    subprocess.Popen(command, shell=True)

My hope was, all of the shell commands would be executed simultaneously but instead, I am noticing that it executes one command at a time.. 我的希望是,所有的shell命令将同时执行,但是我注意到它一次执行一个命令。

How do i execute all the bash commands simultaneously. 我如何同时执行所有bash命令。 Thanks 谢谢

This is where pool comes in, it is designed for just this case. 这就是游泳池的用处,它只是为这种情况而设计的。 It maps many inputs to many threads automatically. 它自动将许多输入映射到许多线程。 Here is a good resource on how to use it. 是有关如何使用它的好资源。

from multiprocessing import Pool

def run_command(path):
    command = "spark-submit driver.py -i {}".format(path)
    subprocess.Popen(command, shell=True)

pool = Pool()
pool.map(run_command, paths)

It will create a thread for every item in paths and, run them all at the same time for the given input 它将为路径中的每个项目创建一个线程,并针对给定的输入同时运行所有线程

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

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