<small id='7zTHC'></small><noframes id='7zTHC'>

  • <tfoot id='7zTHC'></tfoot>

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

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

        Python分别从子进程stdout和stderr读取,同时保留顺序

        时间:2023-07-22

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

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

                  本文介绍了Python分别从子进程stdout和stderr读取,同时保留顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 python 子进程,我正在尝试从中读取输出和错误流.目前我有它的工作,但我只能在读完 stdout 之后才能从 stderr 中读取.这是它的样子:

                  I have a python subprocess that I'm trying to read output and error streams from. Currently I have it working, but I'm only able to read from stderr after I've finished reading from stdout. Here's what it looks like:

                  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                  stdout_iterator = iter(process.stdout.readline, b"")
                  stderr_iterator = iter(process.stderr.readline, b"")
                  
                  for line in stdout_iterator:
                      # Do stuff with line
                      print line
                  
                  for line in stderr_iterator:
                      # Do stuff with line
                      print line
                  

                  如您所见,在 stdout 循环完成之前,stderr for 循环无法启动.我怎样才能修改它以便能够以正确的行进来的顺序从两者中读取?

                  As you can see, the stderr for loop can't start until the stdout loop completes. How can I modify this to be able to read from both in the correct order the lines come in?

                  澄清一下:我仍然需要能够判断一行是来自 stdout 还是 stderr 因为它们在我的代码.

                  To clarify: I still need to be able to tell whether a line came from stdout or stderr because they will be treated differently in my code.

                  推荐答案

                  这是一个基于 selectors 的解决方案,但它保留了顺序,并且流式传输可变长度字符(甚至是单个字符).

                  Here's a solution based on selectors, but one that preserves order, and streams variable-length characters (even single chars).

                  诀窍是使用 read1(),而不是 read().

                  import selectors
                  import subprocess
                  import sys
                  
                  p = subprocess.Popen(
                      ["python", "random_out.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
                  )
                  
                  sel = selectors.DefaultSelector()
                  sel.register(p.stdout, selectors.EVENT_READ)
                  sel.register(p.stderr, selectors.EVENT_READ)
                  
                  while True:
                      for key, _ in sel.select():
                          data = key.fileobj.read1().decode()
                          if not data:
                              exit()
                          if key.fileobj is p.stdout:
                              print(data, end="")
                          else:
                              print(data, end="", file=sys.stderr)
                  

                  如果你想要一个测试程序,使用这个.

                  If you want a test program, use this.

                  import sys
                  from time import sleep
                  
                  
                  for i in range(10):
                      print(f" x{i} ", file=sys.stderr, end="")
                      sleep(0.1)
                      print(f" y{i} ", end="")
                      sleep(0.1)
                  

                  这篇关于Python分别从子进程stdout和stderr读取,同时保留顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:以与长时间运行的 Python 进程不同的用户身份运行子进程 下一篇:以冲突的可执行文件/路径打开

                  相关文章

                  <i id='A8xVG'><tr id='A8xVG'><dt id='A8xVG'><q id='A8xVG'><span id='A8xVG'><b id='A8xVG'><form id='A8xVG'><ins id='A8xVG'></ins><ul id='A8xVG'></ul><sub id='A8xVG'></sub></form><legend id='A8xVG'></legend><bdo id='A8xVG'><pre id='A8xVG'><center id='A8xVG'></center></pre></bdo></b><th id='A8xVG'></th></span></q></dt></tr></i><div id='A8xVG'><tfoot id='A8xVG'></tfoot><dl id='A8xVG'><fieldset id='A8xVG'></fieldset></dl></div>
                2. <tfoot id='A8xVG'></tfoot>
                3. <small id='A8xVG'></small><noframes id='A8xVG'>

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

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