title: How can I read all availably data from subprocess.Popen.stdout (non blocking)?
body: <p>I need a way to either read all currently available characters in stream created by Popen or to find out how many characters are left in the buffer.</p>
  
  <p>Backround:
  I want to remote control an interactive application in Python. So far I used Popen to create a new subprocess:</p>
  
  <pre><code>process=subprocess.Popen(["python"],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, cwd=workingDir)
  </code></pre>
  
  <p>(I'm not really starting python, but the actual interactive interface is similar.)
  At the moment I read 1 byte until I detect that the process has reached the command prompt:</p>
  
  <pre><code>output = ""
  while output[-6:]!="SCIP&gt; ":
      output += process.stdout.read(1)
      sys.stdout.write(output[-1])
  return output
  </code></pre>
  
  <p>Then I start a lengthy computation via <code>process.stdin.write("command\n")</code>.
  My problem is, that I cannot check whether the computation has finished or not, because I cannot check, whether the last characters in the stream are the prompt or not. <code>read()</code> or <code>read(n)</code> blocks my thread until it reaches EOF, which it never will, because the interactive program will not end until it is told to. Looking for the prompt in the way the above loop does won't work either, because the prompt will only occur after the computation.</p>
  
  <p>The ideal solution would allow me to read all available character from the stream and immediately return an empty string, if there is nothing to read.</p>