About piping stdio and subprocess.Popen
Question: I have one Python program, that is opening another Python program via
`subprocess.Popen`. 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 (`read()` from it), and print
that response.

The 2nd one is supposed to listen to the first one's input (via `raw_input()`)
and then `print` text to the 1st.

To understand what exactly was happening, I had put a 5 second delay into the
2nd, and the result surprised me a bit.

Here's the code:

    
    
    import subprocess
    
    print "1st starting."
    
    app = subprocess.Popen("name", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) #<--- B
    
    print "Writing something to app's STDIN..."
    app.stdin.write(some_text)
    
    print "Reading something from my STDIN..." #<--- A
    result = app.stdout.read()
    
    print "Result:"
    print result
    

And for the 2nd one:

    
    
    import time
    
    print "app invoked."
    
    print "Waiting for text from STDIN..."
    text = raw_input()
    
    #process(text)
    time.sleep(5)
    
    print "magic"
    

When I ran this code, it paused at point A, as that was the last console
output.

After 5 seconds, the `"Result:\n"` line would be outputted, and everything the
2nd program had `print`ed would show up in the console.

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?

I'm running Debian Linux 7.0.


Answer: You are asking program 1 to read input from program 2. And you are pausing
program two for five seconds before it outputs anything. Obviously program 1
then needs to wait those five seconds. So what happens is perfectly expected.

> Does it have to wait for its child to terminate before reading its output?

To some extent, yes, because input and output is buffered, so it's possible
that even if you move the delay to _after_ you print something the same will
happen.

`raw_input()` will wait for a linefeed, in any case.

