title: real time subprocess.Popen via stdout and PIPE
body: <p>I am trying to grab <code>stdout</code> from a <code>subprocess.Popen</code> call and although I am achieving this easily by doing:</p>
  
  <pre><code>cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE)
  for line in cmd.stdout.readlines():
      print line
  </code></pre>
  
  <p>I would like to grab <code>stdout</code> in "real time". With the above method, PIPE is waiting to grab all the <code>stdout</code> and then it returns.</p>
  
  <p>So for logging purposes, this doesn't meet my requirements (e.g. "see" what is going on while it happens).</p>
  
  <p>Is there a way to get line by line, <code>stdout</code> while is running? Or is this a limitation of <code>subprocess</code>(having to wait until the <code>PIPE</code> closes).</p>
  
  <p><em>EDIT</em>
  If I switch <code>readlines()</code> for <code>readline()</code> I only get the last line of the <code>stdout</code> (not ideal):</p>
  
  <pre><code>In [75]: cmd = Popen('ls -l', shell=True, stdout=PIPE)
  In [76]: for i in cmd.stdout.readline(): print i
  ....: 
  t
  o
  t
  a
  l
  
  1
  0
  4
  </code></pre>