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

      1. <tfoot id='waWG3'></tfoot>

        <legend id='waWG3'><style id='waWG3'><dir id='waWG3'><q id='waWG3'></q></dir></style></legend>
      2. <small id='waWG3'></small><noframes id='waWG3'>

        • <bdo id='waWG3'></bdo><ul id='waWG3'></ul>
      3. subprocess.Popen.stdout - 实时读取标准输出(再次)

        时间:2023-07-21
        <i id='YrkPl'><tr id='YrkPl'><dt id='YrkPl'><q id='YrkPl'><span id='YrkPl'><b id='YrkPl'><form id='YrkPl'><ins id='YrkPl'></ins><ul id='YrkPl'></ul><sub id='YrkPl'></sub></form><legend id='YrkPl'></legend><bdo id='YrkPl'><pre id='YrkPl'><center id='YrkPl'></center></pre></bdo></b><th id='YrkPl'></th></span></q></dt></tr></i><div id='YrkPl'><tfoot id='YrkPl'></tfoot><dl id='YrkPl'><fieldset id='YrkPl'></fieldset></dl></div>
        • <tfoot id='YrkPl'></tfoot>

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

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

              <bdo id='YrkPl'></bdo><ul id='YrkPl'></ul>
                    <tbody id='YrkPl'></tbody>
                  本文介绍了subprocess.Popen.stdout - 实时读取标准输出(再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  同样的问题.
                  原因是 - 阅读以下内容后我仍然无法使其工作:

                  Again, the same question.
                  The reason is - I still can't make it work after reading the following:

                  • 在Python中实时截取另一个进程的stdout
                  • 在子进程运行时拦截其标准输出
                  • 如何从 subprocess.Popen 在 python (2.5) 中获取实时"信息
                  • 从子进程中实时捕获标准输出

                  我的情况是我有一个用 C 编写的控制台应用程序,让我们以循环中的代码为例:

                  My case is that I have a console app written in C, lets take for example this code in a loop:

                  tmp = 0.0;   
                  printf("
                  input>>"); 
                  scanf_s("%f",&tmp); 
                  printf ("
                  input was: %f",tmp); 
                  

                  它不断地读取一些输入并写入一些输出.

                  It continuously reads some input and writes some output.

                  我与之交互的python代码如下:

                  My python code to interact with it is the following:

                  p=subprocess.Popen([path],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
                  p.stdin.write('12345
                  ')
                  for line in p.stdout: 
                      print(">>> " + str(line.rstrip())) 
                      p.stdout.flush() 
                  

                  到目前为止,每当我阅读表单 p.stdout 时,它总是等到进程终止,然后输出一个空字符串.我已经尝试了很多东西 - 但仍然是相同的结果.

                  So far whenever I read form p.stdout it always waits until the process is terminated and then outputs an empty string. I've tried lots of stuff - but still the same result.

                  我尝试了 Python 2.6 和 3.1,但版本无关紧要 - 我只需要让它在某个地方工作.

                  I tried Python 2.6 and 3.1, but the version doesn't matter - I just need to make it work somewhere.

                  推荐答案

                  尝试在管道中写入和读取到子进程是很棘手的,因为默认的缓冲是双向进行的.当一个或另一个进程(父进程或子进程)正在从空缓冲区读取、写入完整缓冲区或在系统库刷新数据之前对等待数据的缓冲区进行阻塞读取时,很容易出现死锁.

                  Trying to write to and read from pipes to a sub-process is tricky because of the default buffering going on in both directions. It's extremely easy to get a deadlock where one or the other process (parent or child) is reading from an empty buffer, writing into a full buffer or doing a blocking read on a buffer that's awaiting data before the system libraries flush it.

                  对于更少量的数据,Popen.communicate() 方法可能就足够了.但是,对于超出其缓冲的数据,您可能会遇到停滞的进程(类似于您已经看到的?)

                  For more modest amounts of data the Popen.communicate() method might be sufficient. However, for data that exceeds its buffering you'd probably get stalled processes (similar to what you're already seeing?)

                  您可能想了解有关使用 fcntl 模块并使文件描述符中的一个或另一个(或两者)非阻塞的详细信息.当然,在这种情况下,您必须将所有对这些文件描述符的读取和/或写入包装在适当的异常处理中,以处理EWOULDBLOCK"事件.(我不记得为这些引发的确切 Python 异常).

                  You might want to look for details on using the fcntl module and making one or the other (or both) of your file descriptors non-blocking. In that case, of course, you'll have to wrap all reads and/or writes to those file descriptors in the appropriate exception handling to handle the "EWOULDBLOCK" events. (I don't remember the exact Python exception that's raised for these).

                  一个完全不同的方法是让你的父母使用 select 模块和 os.fork() ... 并且让子进程 execve() 直接处理任何文件 dup() 之后的目标程序.(基本上,您将重新实现 Popen() 的部分,但使用不同的父文件描述符 (PIPE) 处理.

                  A completely different approach would be for your parent to use the select module and os.fork() ... and for the child process to execve() the target program after directly handling any file dup()ing. (Basically you'd be re-implement parts of Popen() but with different parent file descriptor (PIPE) handling.

                  顺便说一句,.communicate,至少在 Python 的 2.5 和 2.6 标准库中,只能处理大约 64K 的远程数据(在 Linux 和 FreeBSD 上).这个数字可能会因各种因素而异(可能包括用于编译 Python 解释器的构建选项,或者链接到它的 libc 版本).它不仅仅受限于可用内存(尽管 J.F. Sebastian 断言相反),而是受限于更小的值.

                  Incidentally, .communicate, at least in Python's 2.5 and 2.6 standard libraries, will only handle about 64K of remote data (on Linux and FreeBSD). This number may vary based on various factors (possibly including the build options used to compile your Python interpreter, or the version of libc being linked to it). It is NOT simply limited by available memory (despite J.F. Sebastian's assertion to the contrary) but is limited to a much smaller value.

                  这篇关于subprocess.Popen.stdout - 实时读取标准输出(再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python子进程调用返回“command not found",终端正确执行 下一篇:Python os.system 没有输出

                  相关文章

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

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

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