title: About piping stdio and subprocess.Popen
body: <p>I have one Python program, that is opening another Python program via <code>subprocess.Popen</code>. The 1st is supposed to output some text into the console (just for info), and write some text to the 2nd program it had spawned. Then, it should wait for the 2nd program to respond (<code>read()</code> from it), and print that response.</p>
  
  <p>The 2nd one is supposed to listen to the first one's input (via <code>raw_input()</code>) and then <code>print</code> text to the 1st.</p>
  
  <p>To understand what exactly was happening, I had put a 5 second delay into the 2nd, and the result surprised me a bit.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>import subprocess
  
  print "1st starting."
  
  app = subprocess.Popen("name", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) #&lt;--- B
  
  print "Writing something to app's STDIN..."
  app.stdin.write(some_text)
  
  print "Reading something from my STDIN..." #&lt;--- A
  result = app.stdout.read()
  
  print "Result:"
  print result
  </code></pre>
  
  <p>And for the 2nd one:</p>
  
  <pre><code>import time
  
  print "app invoked."
  
  print "Waiting for text from STDIN..."
  text = raw_input()
  
  #process(text)
  time.sleep(5)
  
  print "magic"
  </code></pre>
  
  <p>When I ran this code, it paused at point A, as that was the last console output.</p>
  
  <p>After 5 seconds, the <code>"Result:\n"</code> line would be outputted, and everything the 2nd program had <code>print</code>ed would show up in the console.</p>
  
  <p>Why did the 1st program pause when reading the stdout of the 2nd one? Does it have to wait for its child to terminate before reading its output? How can this be changed so I can pass messages between programs?</p>
  
  <p>I'm running Debian Linux 7.0.</p>