<small id='mo5CL'></small><noframes id='mo5CL'>

  • <legend id='mo5CL'><style id='mo5CL'><dir id='mo5CL'><q id='mo5CL'></q></dir></style></legend>
  • <tfoot id='mo5CL'></tfoot>
  • <i id='mo5CL'><tr id='mo5CL'><dt id='mo5CL'><q id='mo5CL'><span id='mo5CL'><b id='mo5CL'><form id='mo5CL'><ins id='mo5CL'></ins><ul id='mo5CL'></ul><sub id='mo5CL'></sub></form><legend id='mo5CL'></legend><bdo id='mo5CL'><pre id='mo5CL'><center id='mo5CL'></center></pre></bdo></b><th id='mo5CL'></th></span></q></dt></tr></i><div id='mo5CL'><tfoot id='mo5CL'></tfoot><dl id='mo5CL'><fieldset id='mo5CL'></fieldset></dl></div>

        <bdo id='mo5CL'></bdo><ul id='mo5CL'></ul>

        如何并行运行多个子进程并等待它们在 Python 中完

        时间:2023-08-06

          <small id='cEz36'></small><noframes id='cEz36'>

            <tbody id='cEz36'></tbody>

                <i id='cEz36'><tr id='cEz36'><dt id='cEz36'><q id='cEz36'><span id='cEz36'><b id='cEz36'><form id='cEz36'><ins id='cEz36'></ins><ul id='cEz36'></ul><sub id='cEz36'></sub></form><legend id='cEz36'></legend><bdo id='cEz36'><pre id='cEz36'><center id='cEz36'></center></pre></bdo></b><th id='cEz36'></th></span></q></dt></tr></i><div id='cEz36'><tfoot id='cEz36'></tfoot><dl id='cEz36'><fieldset id='cEz36'></fieldset></dl></div>
                <legend id='cEz36'><style id='cEz36'><dir id='cEz36'><q id='cEz36'></q></dir></style></legend>
                • <bdo id='cEz36'></bdo><ul id='cEz36'></ul>
                  <tfoot id='cEz36'></tfoot>
                  本文介绍了如何并行运行多个子进程并等待它们在 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 中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:EOFError:类内的输入用完 下一篇:python multiprocessing.Pool kill *特定*长时间运行或挂起

                  相关文章

                  最新文章

                • <i id='9muHF'><tr id='9muHF'><dt id='9muHF'><q id='9muHF'><span id='9muHF'><b id='9muHF'><form id='9muHF'><ins id='9muHF'></ins><ul id='9muHF'></ul><sub id='9muHF'></sub></form><legend id='9muHF'></legend><bdo id='9muHF'><pre id='9muHF'><center id='9muHF'></center></pre></bdo></b><th id='9muHF'></th></span></q></dt></tr></i><div id='9muHF'><tfoot id='9muHF'></tfoot><dl id='9muHF'><fieldset id='9muHF'></fieldset></dl></div>

                    <bdo id='9muHF'></bdo><ul id='9muHF'></ul>
                    1. <legend id='9muHF'><style id='9muHF'><dir id='9muHF'><q id='9muHF'></q></dir></style></legend>

                      <small id='9muHF'></small><noframes id='9muHF'>

                      <tfoot id='9muHF'></tfoot>