如何并行运行多个子流程,然后等待它们在Python中完成 [英] How do I run multiple subprocesses in parallel and wait for them to finish in Python

查看:102
本文介绍了如何并行运行多个子流程,然后等待它们在Python中完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将bash脚本迁移到Python.

bash脚本并行运行多个OS命令,然后等待它们完成再恢复,即:

command1&

command2&

.

commandn&

等待

命令

我想使用Python子过程实现相同的目的.这可能吗?如何在继续之前等待subprocess.call命令完成?

解决方案

您仍然可以使用Popen,它的输入参数与subprocess.call相同,但更加灵活.

subprocess.call:全功能签名与Popen构造函数的签名相同-此函数将所有提供的参数直接传递到该接口.

一个区别是subprocess.call阻塞并等待子进程完成(它建立在Popen之上),而Popen不阻塞,因此允许您并行启动其他进程. /p>

尝试以下操作:

from subprocess import Popen
commands = ['command1', 'command2']
procs = [ Popen(i) for i in commands ]
for p in procs:
   p.wait()

I am trying to migrate a bash script to Python.

The bash script runs multiple OS commands in parallel then waits for them to finish before resuming, ie:

command1 &

command2 &

.

commandn &

wait

command

I want to achieve the same using Python subprocess. Is this possible? How can I wait for a subprocess.call command to finish before resuming?

解决方案

You can still use Popen which takes the same input parameters as subprocess.call but is more flexible.

subprocess.call: The full function signature is the same as that of the Popen constructor - this functions passes all supplied arguments directly through to that interface.

One difference is that subprocess.call blocks and waits for the subprocess to complete (it is built on top of Popen), whereas Popen doesn't block and consequently allows you to launch other processes in parallel.

Try the following:

from subprocess import Popen
commands = ['command1', 'command2']
procs = [ Popen(i) for i in commands ]
for p in procs:
   p.wait()

这篇关于如何并行运行多个子流程,然后等待它们在Python中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆