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

      <small id='77f2B'></small><noframes id='77f2B'>

    1. <tfoot id='77f2B'></tfoot>

      你能像往常一样制作一个python子进程输出stdout和stderr,但也可以将输出捕获为字符串吗?

      Can you make a python subprocess output stdout and stderr as usual, but also capture the output as a string?(你能像往常一样制作一个python子进程输出stdout和stderr,但也可以将输出捕获为字符串吗?)

        <tbody id='ak24j'></tbody>
          <legend id='ak24j'><style id='ak24j'><dir id='ak24j'><q id='ak24j'></q></dir></style></legend>

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

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

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

                本文介绍了你能像往常一样制作一个python子进程输出stdout和stderr,但也可以将输出捕获为字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                可能重复:
                包装子进程'stdout/stderr

                在这个问题中,hanan-n 询问是否有可能有一个 python 子进程输出到标准输出,同时还将输出保存在一个字符串中以供以后处理.这种情况下的解决方案是遍历每个输出行并手动打印它们:

                In this question, hanan-n asked whether it was possible to have a python subprocess that outputs to stdout while also keeping the output in a string for later processing. The solution in this case was to loop over every output line and print them manually:

                output = []
                p = subprocess.Popen(["the", "command"], stdout=subprocess.PIPE)
                for line in iter(p.stdout.readline, ''):
                    print(line)
                    output.append(line)
                

                但是,此解决方案并不适用于您希望同时为 stdout 和 stderr 执行此操作的情况,同时满足以下条件:

                However, this solution doesn't generalise to the case where you want to do this for both stdout and stderr, while satisfying the following:

                • stdout/stderr 的输出应该分别到父进程的stdout/stderr
                • 输出应尽可能实时完成(但我只需要访问最后的字符串)
                • 不应更改 stdout 和 stderr 行之间的顺序(我不太确定如果子进程以不同的时间间隔刷新其 stdout 和 stderr 缓存,这将如何工作;让我们现在假设我们将所有内容都放在很好的块中包含完整的行?)

                我查看了 子流程文档,但找不到任何可以做到这一点.我能找到的最接近的方法是添加 stderr=subprocess.stdout 并使用与上面相同的解决方案,但是我们失去了常规输出和错误之间的区别.有任何想法吗?我猜测解决方案 - 如果有的话 - 将涉及对 p.stdoutp.stderr 进行异步读取.

                I looked through the subprocess documentation, but couldn't find anything that can achieve this. The closest I could find is to add stderr=subprocess.stdout and use the same solution as above, but then we lose the distinction between regular output and errors. Any ideas? I'm guessing the solution - if there is one - will involve having asynchronous reads to p.stdout and p.stderr.

                这是我想做的一个例子:

                Here is an example of what I would like to do:

                p = subprocess.Popen(["the", "command"])
                p.wait()  # while p runs, the command's stdout and stderr should behave as usual
                p_stdout = p.stdout.read()  # unfortunately, this will return '' unless you use subprocess.PIPE
                p_stderr = p.stderr.read()  # ditto
                [do something with p_stdout and p_stderr]
                

                推荐答案

                这个例子似乎对我有用:

                This example seems to work for me:

                # -*- Mode: Python -*-
                # vi:si:et:sw=4:sts=4:ts=4
                
                import subprocess
                import sys
                import select
                
                p = subprocess.Popen(["find", "/proc"],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                
                stdout = []
                stderr = []
                
                while True:
                    reads = [p.stdout.fileno(), p.stderr.fileno()]
                    ret = select.select(reads, [], [])
                
                    for fd in ret[0]:
                        if fd == p.stdout.fileno():
                            read = p.stdout.readline()
                            sys.stdout.write('stdout: ' + read)
                            stdout.append(read)
                        if fd == p.stderr.fileno():
                            read = p.stderr.readline()
                            sys.stderr.write('stderr: ' + read)
                            stderr.append(read)
                
                    if p.poll() != None:
                        break
                
                print 'program ended'
                
                print 'stdout:', "".join(stdout)
                print 'stderr:', "".join(stderr)
                

                一般来说,在任何情况下,如果您想同时处理多个文件描述符并且您不知道哪个有内容供您阅读,您应该使用 select 或类似的东西(例如 Twisted reactor).

                In general, any situation where you want to do stuff with multiple file descriptors at the same time and you don't know which one will have stuff for you to read, you should use select or something equivalent (like a Twisted reactor).

                这篇关于你能像往常一样制作一个python子进程输出stdout和stderr,但也可以将输出捕获为字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 画布滚动条?)

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

                    • <tfoot id='8QpVn'></tfoot>

                        <tbody id='8QpVn'></tbody>

                        <small id='8QpVn'></small><noframes id='8QpVn'>

                        <legend id='8QpVn'><style id='8QpVn'><dir id='8QpVn'><q id='8QpVn'></q></dir></style></legend>
                        • <bdo id='8QpVn'></bdo><ul id='8QpVn'></ul>