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

    1. <legend id='qMyUD'><style id='qMyUD'><dir id='qMyUD'><q id='qMyUD'></q></dir></style></legend>
        <bdo id='qMyUD'></bdo><ul id='qMyUD'></ul>

        <tfoot id='qMyUD'></tfoot>
      1. 在运行时拦截子进程的标准输出

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

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

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

                  <tbody id='z426D'></tbody>

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

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

                  本文介绍了在运行时拦截子进程的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果这是我的子流程:

                  import time, sys
                  for i in range(200):
                      sys.stdout.write( 'reading %i
                  '%i )
                      time.sleep(.02)
                  

                  这是控制和修改子进程输出的脚本:

                  And this is the script controlling and modifying the output of the subprocess:

                  import subprocess, time, sys
                  
                  print 'starting'
                      
                  proc = subprocess.Popen(
                      'c:/test_apps/testcr.py',
                      shell=True,
                      stdin=subprocess.PIPE,
                      stdout=subprocess.PIPE  )
                  
                  print 'process created'
                  
                  while True:
                      #next_line = proc.communicate()[0]
                      next_line = proc.stdout.readline()
                      if next_line == '' and proc.poll() != None:
                          break
                      sys.stdout.write(next_line)
                      sys.stdout.flush()
                      
                  print 'done'
                  

                  为什么 readlinecommunicate 一直等到进程完成运行?有没有一种简单的方法可以实时传递(和修改)子进程的标准输出?

                  Why is readline and communicate waiting until the process is done running? Is there a simple way to pass (and modify) the subprocess' stdout real-time?

                  我使用的是 Windows XP.

                  I'm on Windows XP.

                  推荐答案

                  正如 Charles 已经提到的,问题在于缓冲.我在为 SNMPd 编写一些模块时遇到了类似的问题,并通过将 stdout 替换为自动刷新版本来解决它.

                  As Charles already mentioned, the problem is buffering. I ran in to a similar problem when writing some modules for SNMPd, and solved it by replacing stdout with an auto-flushing version.

                  我使用了以下代码,灵感来自 ActiveState 上的一些帖子:

                  I used the following code, inspired by some posts on ActiveState:

                  class FlushFile(object):
                      """Write-only flushing wrapper for file-type objects."""
                      def __init__(self, f):
                          self.f = f
                      def write(self, x):
                          self.f.write(x)
                          self.f.flush()
                  
                  # Replace stdout with an automatically flushing version
                  sys.stdout = FlushFile(sys.__stdout__)
                  

                  这篇关于在运行时拦截子进程的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python subprocess.call 一个 bash 别名 下一篇:子进程 readline 挂起等待 EOF

                  相关文章

                    <tfoot id='Yh0f6'></tfoot>
                  1. <small id='Yh0f6'></small><noframes id='Yh0f6'>

                  2. <legend id='Yh0f6'><style id='Yh0f6'><dir id='Yh0f6'><q id='Yh0f6'></q></dir></style></legend>
                    • <bdo id='Yh0f6'></bdo><ul id='Yh0f6'></ul>

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