• <legend id='I99ng'><style id='I99ng'><dir id='I99ng'><q id='I99ng'></q></dir></style></legend>

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

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

        在python中控制用于调用外部命令的子进程数

        时间:2023-05-26

        <tfoot id='LBmZJ'></tfoot>

          1. <small id='LBmZJ'></small><noframes id='LBmZJ'>

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

                  <tbody id='LBmZJ'></tbody>
              • <i id='LBmZJ'><tr id='LBmZJ'><dt id='LBmZJ'><q id='LBmZJ'><span id='LBmZJ'><b id='LBmZJ'><form id='LBmZJ'><ins id='LBmZJ'></ins><ul id='LBmZJ'></ul><sub id='LBmZJ'></sub></form><legend id='LBmZJ'></legend><bdo id='LBmZJ'><pre id='LBmZJ'><center id='LBmZJ'></center></pre></bdo></b><th id='LBmZJ'></th></span></q></dt></tr></i><div id='LBmZJ'><tfoot id='LBmZJ'></tfoot><dl id='LBmZJ'><fieldset id='LBmZJ'></fieldset></dl></div>
                <legend id='LBmZJ'><style id='LBmZJ'><dir id='LBmZJ'><q id='LBmZJ'></q></dir></style></legend>
                • 本文介绍了在python中控制用于调用外部命令的子进程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我了解使用 subprocess 是调用外部命令的首选方式.

                  I understand using subprocess is the preferred way of calling external command.

                  但是,如果我想并行运行多个命令,但要限制生成的进程数怎么办?困扰我的是我无法阻止子进程.例如,如果我调用

                  But what if I want to run several commands in parall, but limit the number of processes being spawned? What bothers me is that I can't block the subprocesses. For example, if I call

                  subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile)
                  

                  然后该过程将继续,无需等待 cmd 完成.因此,我无法将其包装在 multiprocessing 库的工作人员中.

                  Then the process will continue, without waiting for cmd to finish. Therefore, I can't wrap it up in a worker of multiprocessing library.

                  例如,如果我这样做:

                  def worker(cmd): 
                      subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile);
                  
                  pool = Pool( processes = 10 );
                  results =[pool.apply_async(worker, [cmd]) for cmd in cmd_list];
                  ans = [res.get() for res in results];
                  

                  然后每个工作人员将在生成子进程后完成并返回.所以我不能通过使用Pool 来真正限制subprocess 生成的进程数.

                  then each worker will finish and return after spawning a subprocess. So I can't really limit the number of processes generated by subprocess by using Pool.

                  限制子进程数量的正确方法是什么?

                  What's the proper way of limiting the number of subprocesses?

                  推荐答案

                  如果要等待命令完成,可以使用subprocess.call.有关详细信息,请参阅 pydoc 子进程.

                  You can use subprocess.call if you want to wait for the command to complete. See pydoc subprocess for more information.

                  您也可以调用 Popen.wait 你的工人中的方法:

                  You could also call the Popen.wait method in your worker:

                  def worker(cmd): 
                      p = subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile);
                      p.wait()
                  

                  这篇关于在python中控制用于调用外部命令的子进程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:python多处理管理器列表错误:[Errno 2]没有这样的文 下一篇:python多处理的生产者/消费者问题

                  相关文章

                  最新文章

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

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

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