• <bdo id='e9ZDH'></bdo><ul id='e9ZDH'></ul>

    <tfoot id='e9ZDH'></tfoot>

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

    1. <legend id='e9ZDH'><style id='e9ZDH'><dir id='e9ZDH'><q id='e9ZDH'></q></dir></style></legend>

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

      1. 如何在 Python 2.7 中线程化多个子进程实例?

        How to thread multiple subprocess instances in Python 2.7?(如何在 Python 2.7 中线程化多个子进程实例?)

          <tfoot id='XGEB7'></tfoot>

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

          • <small id='XGEB7'></small><noframes id='XGEB7'>

                • <bdo id='XGEB7'></bdo><ul id='XGEB7'></ul>
                    <tbody id='XGEB7'></tbody>
                • <legend id='XGEB7'><style id='XGEB7'><dir id='XGEB7'><q id='XGEB7'></q></dir></style></legend>
                  本文介绍了如何在 Python 2.7 中线程化多个子进程实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三个命令,否则它们很容易在命令行上链接在一起,如下所示:

                  I have three commands that would otherwise be easily chained together on the command-line like so:

                  $ echo foo | firstCommand - | secondCommand - | thirdCommand - > finalOutput
                  

                  换句话说,firstCommand 处理来自标准输入的 foo 并将结果通过管道传送到 secondCommand,后者依次处理该输入和管道其输出到 thirdCommand,后者进行处理并将其输出重定向到文件 finalOutput.

                  In other words, the firstCommand processes foo from standard input and pipes the result to secondCommand, which in turn processes that input and pipes its output to thirdCommand, which does processing and redirects its output to the file finalOutput.

                  我一直试图在 Python 脚本中使用线程来概括这一点.我想使用 Python 来操作 firstCommand 的输出,然后再将其传递给 secondCommand,并在 secondCommand 之间再次操作第三条命令.

                  I have been trying to recapitulate this in a Python script, using threading. I'd like to use Python in order to manipulate the output from firstCommand before passing it to secondCommand, and again between secondCommand and thirdCommand.

                  这是一段似乎不起作用的代码摘录:

                  Here's an excerpt of code that does not seem to work:

                  first_process = subprocess.Popen(['firstCommand', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  second_process = subprocess.Popen(['secondCommand', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  third_process = subprocess.Popen(['thirdCommand', '-'], stdin=subprocess.PIPE, stdout=sys.stdout)
                  
                  first_thread = threading.Thread(target=consumeOutputFromStdin, args=(sys.stdin, first_process.stdin))
                  second_thread = threading.Thread(target=consumeOutputFromFirstCommand, args=(first_process.stdout, second_process.stdin))
                  third_thread = threading.Thread(target=consumeOutputFromSecondCommand, args=(second_process.stdout, third_process.stdin))
                  
                  first_thread.start()
                  second_thread.start()
                  third_thread.start()
                  
                  first_thread.join()
                  second_thread.join()
                  third_thread.join()
                  
                  first_process.communicate()
                  second_process.communicate()
                  third_process.communicate()
                  
                  # read 1K chunks from standard input
                  def consumeOutputFromStdin(from_stream, to_stream):
                      chunk = from_stream.read(1024)
                      while chunk:
                          to_stream.write(chunk)
                          to_stream.flush()
                          chunk = from_stream.read(1024)
                  
                  def consumeOutputFromFirstCommand(from_stream, to_stream):
                      while True:
                          unprocessed_line = from_stream.readline()
                          if not unprocessed_line:
                              break
                          processed_line = some_python_function_that_processes_line(unprocessed_line)
                          to_stream.write(processed_line)
                          to_stream.flush()
                  
                  def consumeOutputFromSecondCommand(from_stream, to_stream):
                      while True:
                          unprocessed_line = from_stream.readline()
                          if not unprocessed_line:
                              break
                          processed_line = a_different_python_function_that_processes_line(unprocessed_line)
                          to_stream.write(processed_line)
                          to_stream.flush()
                  

                  当我运行它时,脚本挂起:

                  When I run this, the script hangs:

                  $ echo foo | ./myConversionScript.py
                  ** hangs here... **
                  

                  如果我按 Ctrl-C 来终止脚本,代码会卡在 third_thread.join() 行:

                  If I hit Ctrl-C to terminate the script, the code is stuck on the line third_thread.join():

                    C-c C-c
                  Traceback (most recent call last):
                    File "./myConversionScript.py", line 786, in <module>
                      sys.exit(main(*sys.argv))
                    File "./myConversionScript.py", line 556, in main
                      third_thread.join()
                    File "/home/foo/proj/tools/lib/python2.7/threading.py", line 949, in join
                      self.__block.wait()
                    File "/home/foo/proj/tools/lib/python2.7/threading.py", line 339, in wait
                      waiter.acquire()
                  KeyboardInterrupt
                  

                  如果我不使用 third_processthird_thread,而只是将数据从第一个线程的输出传递到第二个线程的输入,则没有挂起.

                  If I don't use a third_process and third_thread, instead only passing data from the output of the first thread to the input of the second thread, there is no hang.

                  关于第三个线程的某些东西似乎导致事情中断,但我不知道为什么.

                  Something about the third thread seems to cause things to break, but I don't know why.

                  我认为 communicate() 的重点是它将处理三个进程的 I/O,所以我不确定为什么会出现 I/O 挂起.

                  I thought the point of communicate() is that it will handle I/O for the three processes, so I'm not sure why there is an I/O hang.

                  如何让三个或更多命令/进程一起工作,其中一个线程消耗另一个线程/进程的输出?

                  How do I get three or more commands/processes working together, where one thread consumes the output of another thread/process?

                  更新

                  好的,根据此处和其他网站上的一些评论,我做了一些似乎有帮助的更改.进程将在 wait() 中完成,并且在线程方法中,一旦线程处理了它可以处理的所有数据,我就会 close() 管道.我担心大型数据集的内存使用率会很高,但至少一切正常:

                  Okay, I made some changes that seem to help, based on some comments here and on other sites. The processes are made to wait() for completion, and within the thread methods, I close() the pipes once the thread has processed all the data that it can. My concern is that memory usage will be very high for large datasets, but at least things are working:

                  first_process = subprocess.Popen(['firstCommand', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  second_process = subprocess.Popen(['secondCommand', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  third_process = subprocess.Popen(['thirdCommand', '-'], stdin=subprocess.PIPE, stdout=sys.stdout)
                  
                  first_thread = threading.Thread(target=consumeOutputFromStdin, args=(sys.stdin, first_process.stdin))
                  second_thread = threading.Thread(target=consumeOutputFromFirstCommand, args=(first_process.stdout, second_process.stdin))
                  third_thread = threading.Thread(target=consumeOutputFromSecondCommand, args=(second_process.stdout, third_process.stdin))
                  
                  first_thread.start()
                  second_thread.start()
                  third_thread.start()
                  
                  first_thread.join()
                  second_thread.join()
                  third_thread.join()
                  
                  first_process.wait()
                  second_process.wait()
                  third_process.wait()
                  
                  # read 1K chunks from standard input
                  def consumeOutputFromStdin(from_stream, to_stream):
                      chunk = from_stream.read(1024)
                      while chunk:
                          to_stream.write(chunk)
                          to_stream.flush()
                          chunk = from_stream.read(1024)
                  
                  def consumeOutputFromFirstCommand(from_stream, to_stream):
                      while True:
                          unprocessed_line = from_stream.readline()
                          if not unprocessed_line:
                              from_stream.close()
                              to_stream.close()
                              break
                          processed_line = some_python_function_that_processes_line(unprocessed_line)
                          to_stream.write(processed_line)
                          to_stream.flush()
                  
                  def consumeOutputFromSecondCommand(from_stream, to_stream):
                      while True:
                          unprocessed_line = from_stream.readline()
                          if not unprocessed_line:
                              from_stream.close()
                              to_stream.close()
                              break
                          processed_line = a_different_python_function_that_processes_line(unprocessed_line)
                          to_stream.write(processed_line)
                          to_stream.flush()
                  

                  推荐答案

                  模拟:

                  echo foo |
                  firstCommand - | somePythonRoutine - |
                  secondCommand - | anotherPythonRoutine - |
                  thirdCommand - > finalOutput
                  

                  您当前使用线程的方法有效:

                  your current approach with threads works:

                  from subprocess import Popen, PIPE
                  
                  first = Popen(["firstCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
                  second = Popen(["secondCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
                  bind(first.stdout, second.stdin, somePythonRoutine)
                  with open("finalOutput", "wb") as file:
                      third = Popen(["thirdCommand", "-"], stdin=PIPE, stdout=file, bufsize=1)
                  bind(second.stdout, third.stdin, anotherPythonRoutine)
                  
                  # provide input for the pipeline
                  first.stdin.write(b"foo")
                  first.stdin.close()
                  
                  # wait for it to complete
                  pipestatus = [p.wait() for p in [first, second, third]]
                  

                  每个 bind() 开始一个新线程的地方:

                  where each bind() starts a new thread:

                  from threading import Thread
                  
                  def bind(input_pipe, output_pipe, line_filter):
                      def f():
                          try:
                              for line in iter(input_pipe.readline, b''):
                                  line = line_filter(line)
                                  if line:
                                      output_pipe.write(line) # no flush unless newline present
                          finally:
                              try:
                                  output_pipe.close()
                              finally:
                                  input_pipe.close()
                      t = Thread(target=f)
                      t.daemon = True # die if the program exits
                      t.start()
                  

                  somePythonRoutineanotherPythonRoutine 接受单行并返回(可能已修改).

                  and somePythonRoutine, anotherPythonRoutine accept a single line and return it (possibly modified).

                  这篇关于如何在 Python 2.7 中线程化多个子进程实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <legend id='HaJd4'><style id='HaJd4'><dir id='HaJd4'><q id='HaJd4'></q></dir></style></legend>

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

                    <tbody id='HaJd4'></tbody>

                  • <tfoot id='HaJd4'></tfoot>

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