title: Unable to read stdout from a running process
body: <p>I have read and tried 8 different methods answered on several questions about this.  I opened a process in python and want to read its output even if the process hasn't terminated yet.  The process doesn't complete usually for at least 1 minute or until an interrupt is sent.  No matter what I try, I can't get it to read the output.  I know the command and args I passed work because when I change it to subprocess.call(cmd, args), it prints everything to the screen.  I also checked that the process is running with ps -ax.  Here is an example of what I'm trying (cat /dev/random is UNRELATED to my project):</p>
  
  <pre><code>proc = subprocess.Popen(["cat", "/dev/random"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  print("Process started.")
  </code></pre>
  
  <p>Here is what I have tried that has failed so far:</p>
  
  <pre><code>for line in iter(p.stdout.readline, ''):
      strLine = str(line).rstrip()
      print("&gt;&gt;&gt; " + strLine )
      sys.stdout.flush()
  </code></pre>
  
  <p>And</p>
  
  <pre><code>output, error = proc.communicate()
  print output
  </code></pre>
  
  <p>And</p>
  
  <pre><code>while proc.poll() is None:
      print("Still waiting.")
      print(proc.stdout.readline(1))
  </code></pre>
  
  <p>There are more solutions I tried that are variations of this but no luck.  When the call function is used without changing stdout, everything prints correctly to the console.  What am I doing wrong?</p>
  
  <p>I'm using Python 2.6.</p>