如何并行运行多个子流程,然后等待它们在Python中完成 [英] How do I run multiple subprocesses in parallel and wait for them to finish in 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屋!