title: Getting realtime output using subprocess
body: <p>I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation.  This requires me to be able to see each line of output from the wrapped program as soon as it is output.</p>
  
  <p>I figured that I'd just execute the program using <code>subprocess.Popen</code>, use <code>stdout=PIPE</code>, then read each line as it came in and act on it accordingly.  However, when I ran the following code, the output appeared to be buffered somewhere, causing it to appear in two chunks, lines 1 through 332, then 333 through 439 (the last line of output)</p>
  
  <pre><code>from subprocess import Popen, PIPE, STDOUT
  
  p = Popen('svnadmin verify /var/svn/repos/config', stdout = PIPE, 
          stderr = STDOUT, shell = True)
  for line in p.stdout:
      print line.replace('\n', '')
  </code></pre>
  
  <p>After looking at the documentation on subprocess a little, I discovered the <code>bufsize</code> parameter to <code>Popen</code>, so I tried setting bufsize to 1 (buffer each line) and 0 (no buffer), but neither value seemed to change the way the lines were being delivered.</p>
  
  <p>At this point I was starting to grasp for straws, so I wrote the following output loop:</p>
  
  <pre><code>while True:
      try:
          print p.stdout.next().replace('\n', '')
      except StopIteration:
          break
  </code></pre>
  
  <p>but got the same result.</p>
  
  <p>Is it possible to get 'realtime' program output of a program executed using subprocess?  Is there some other option in Python that is forward-compatible (not <code>exec*</code>)?</p>