How do I run multiple subprocesses in parallel and wait for them to finish in Python(如何并行运行多个子进程并等待它们在 Python 中完成)
问题描述
我正在尝试将 bash 脚本迁移到 Python.
I am trying to migrate a bash script to Python.
bash 脚本并行运行多个操作系统命令,然后等待它们完成后再恢复,即:
The bash script runs multiple OS commands in parallel then waits for them to finish before resuming, ie:
command1 &
command1 &
command2 &
command2 &
.
命令&
等待
命令
我想使用 Python 子进程实现相同的目的.这可能吗?如何在恢复之前等待 subprocess.call 命令完成?
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?
推荐答案
你仍然可以使用 Popen
,它的输入参数与 subprocess.call
相同,但更灵活.
You can still use Popen
which takes the same input parameters as subprocess.call
but is more flexible.
subprocess.call
:完整的函数签名与 Popen 构造函数的签名相同 - 此函数将所有提供的参数直接传递到该接口.
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.
一个区别是 subprocess.call
阻塞并等待子进程完成(它建立在 Popen
之上),而 Popen
不会阻塞,因此允许您并行启动其他进程.
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.
尝试以下方法:
from subprocess import Popen
commands = ['command1', 'command2']
procs = [ Popen(i) for i in commands ]
for p in procs:
p.wait()
这篇关于如何并行运行多个子进程并等待它们在 Python 中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何并行运行多个子进程并等待它们在 Python 中完成


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01