<tfoot id='6FGW4'></tfoot>

  1. <small id='6FGW4'></small><noframes id='6FGW4'>

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

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

      如何从 subprocess.Popen.stdout 读取所有可用数据(非阻塞)?

      How can I read all availably data from subprocess.Popen.stdout (non blocking)?(如何从 subprocess.Popen.stdout 读取所有可用数据(非阻塞)?)

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

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

              <tbody id='w2ziP'></tbody>

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

              <legend id='w2ziP'><style id='w2ziP'><dir id='w2ziP'><q id='w2ziP'></q></dir></style></legend>
                本文介绍了如何从 subprocess.Popen.stdout 读取所有可用数据(非阻塞)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要一种方法来读取 Popen 创建的流中所有当前可用的字符,或者找出缓冲区中剩余的字符数.

                I need a way to either read all currently available characters in stream created by Popen or to find out how many characters are left in the buffer.

                背景:我想用 Python 远程控制一个交互式应用程序.到目前为止,我使用 Popen 创建了一个新的子进程:

                Backround: I want to remote control an interactive application in Python. So far I used Popen to create a new subprocess:

                process=subprocess.Popen(["python"],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, cwd=workingDir)
                

                (我不是真的开始python,但实际的交互界面是相似的.)目前我读取了 1 个字节,直到我检测到进程已到达命令提示符:

                (I'm not really starting python, but the actual interactive interface is similar.) At the moment I read 1 byte until I detect that the process has reached the command prompt:

                output = ""
                while output[-6:]!="SCIP> ":
                    output += process.stdout.read(1)
                    sys.stdout.write(output[-1])
                return output
                

                然后我通过 process.stdin.write("command ") 开始一个冗长的计算.我的问题是,我无法检查计算是否完成,因为我无法检查流中的最后一个字符是否是提示符.read()read(n) 阻塞我的线程,直到它到达 EOF,它永远不会,因为交互式程序在被告知之前不会结束.以上述循环的方式寻找提示也行不通,因为提示只会在计算之后出现.

                Then I start a lengthy computation via process.stdin.write("command "). My problem is, that I cannot check whether the computation has finished or not, because I cannot check, whether the last characters in the stream are the prompt or not. read() or read(n) blocks my thread until it reaches EOF, which it never will, because the interactive program will not end until it is told to. Looking for the prompt in the way the above loop does won't work either, because the prompt will only occur after the computation.

                理想的解决方案是让我从流中读取所有可用字符,如果没有可读取的内容,则立即返回一个空字符串.

                The ideal solution would allow me to read all available character from the stream and immediately return an empty string, if there is nothing to read.

                推荐答案

                四处寻找这个非常好的解决方案

                Poking around I found this really nice solution

                持久python子进程

                通过使用 fcntl 将子进程管道上的文件属性设置为非阻塞模式,从而避免了阻塞问题,无需辅助线程或轮询.我可能遗漏了一些东西,但它解决了我的交互式过程控制问题.

                which avoids the blocking issue all together by using fcntl to set file attributes on the subprocess pipes to non-blocking mode, no auxiliary threads or polling required. I could be missing something but it has solved my interactive process control problem.

                这篇关于如何从 subprocess.Popen.stdout 读取所有可用数据(非阻塞)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

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

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

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

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