<bdo id='0sl9h'></bdo><ul id='0sl9h'></ul>
      <tfoot id='0sl9h'></tfoot>

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

        <small id='0sl9h'></small><noframes id='0sl9h'>

        并行的 Python 子进程

        时间:2023-07-22

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

                <tbody id='bVkvX'></tbody>
            • <small id='bVkvX'></small><noframes id='bVkvX'>

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

                  本文介绍了并行的 Python 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想并行运行多个进程,并能够随时获取标准输出.我该怎么做?我需要为每个 subprocess.Popen() 调用运行线程吗?

                  I want to run many processes in parallel with ability to take stdout in any time. How should I do it? Do I need to run thread for each subprocess.Popen() call, a what?

                  推荐答案

                  你可以在一个线程中完成.

                  You can do it in a single thread.

                  假设您有一个随机打印行的脚本:

                  Suppose you have a script that prints lines at random times:

                  #!/usr/bin/env python
                  #file: child.py
                  import os
                  import random
                  import sys
                  import time
                  
                  for i in range(10):
                      print("%2d %s %s" % (int(sys.argv[1]), os.getpid(), i))
                      sys.stdout.flush()
                      time.sleep(random.random())
                  

                  并且您想在输出可用时立即收集输出,您可以使用 select 在 POSIX 系统上作为 @zigg建议:

                  And you'd like to collect the output as soon as it becomes available, you could use select on POSIX systems as @zigg suggested:

                  #!/usr/bin/env python
                  from __future__ import print_function
                  from select     import select
                  from subprocess import Popen, PIPE
                  
                  # start several subprocesses
                  processes = [Popen(['./child.py', str(i)], stdout=PIPE,
                                     bufsize=1, close_fds=True,
                                     universal_newlines=True)
                               for i in range(5)]
                  
                  # read output
                  timeout = 0.1 # seconds
                  while processes:
                      # remove finished processes from the list (O(N**2))
                      for p in processes[:]:
                          if p.poll() is not None: # process ended
                              print(p.stdout.read(), end='') # read the rest
                              p.stdout.close()
                              processes.remove(p)
                  
                      # wait until there is something to read
                      rlist = select([p.stdout for p in processes], [],[], timeout)[0]
                  
                      # read a line from each process that has output ready
                      for f in rlist:
                          print(f.readline(), end='') #NOTE: it can block
                  

                  更便携的解决方案(应该适用于 Windows、Linux、OSX)可以为每个进程使用读取器线程,请参阅 非阻塞在 python 中读取 subprocess.PIPE.

                  A more portable solution (that should work on Windows, Linux, OSX) can use reader threads for each process, see Non-blocking read on a subprocess.PIPE in python.

                  这里是 os.pipe() 基于 Unix 和 Windows 的解决方案:

                  Here's os.pipe()-based solution that works on Unix and Windows:

                  #!/usr/bin/env python
                  from __future__ import print_function
                  import io
                  import os
                  import sys
                  from subprocess import Popen
                  
                  ON_POSIX = 'posix' in sys.builtin_module_names
                  
                  # create a pipe to get data
                  input_fd, output_fd = os.pipe()
                  
                  # start several subprocesses
                  processes = [Popen([sys.executable, 'child.py', str(i)], stdout=output_fd,
                                     close_fds=ON_POSIX) # close input_fd in children
                               for i in range(5)]
                  os.close(output_fd) # close unused end of the pipe
                  
                  # read output line by line as soon as it is available
                  with io.open(input_fd, 'r', buffering=1) as file:
                      for line in file:
                          print(line, end='')
                  #
                  for p in processes:
                      p.wait()
                  

                  这篇关于并行的 Python 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 python 子进程之间传递大型 numpy 数组而不保存到磁盘? 下一篇:子进程“TypeError:需要一个类似字节的对象,而不是'str'"

                  相关文章

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

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

                  <tfoot id='N7vuR'></tfoot>
                    <bdo id='N7vuR'></bdo><ul id='N7vuR'></ul>

                      <legend id='N7vuR'><style id='N7vuR'><dir id='N7vuR'><q id='N7vuR'></q></dir></style></legend>