why doesn't subprocess.Popen(...) always return?
Question: I hope this is a simple python question.

When I try the following in the python interpreter:

    
    
    >>> import process
    >>> def test(cmd):
    ...   p = subprocess.Popen(cmd)
    ...
    >>> test(['ls', '-l'])
    

It will run the `ls -l`, but I need to hit "return" to get a new >>> prompt.

However, when I try the following:

    
    
    >>> import process
    >>> def test(cmd):
    ...   p = subprocess.Popen(cmd)
    ...   p.wait()
    ...
    >>> test(['ls', '-l'])
    

Then the `ls -l` will be run with a >>> prompt immediately present.

One other variation:

    
    
    >>> import process
    >>> def test(cmd):
    ...   p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    ...
    >>> test(['ls', '-l'])
    

This will give me an immediate new prompt.

The last example is closest to what I want. My goal is to launch a child
process, wait for it to finish and then use its stdout in my parent process by
referring to `p.stdout` while letting stderr just print to wherever it would
otherwise.

Right now in my actual application, the last version just hangs at the: `p =
subprocess.Popen(cmd, stdout=subprocess.PIPE)` with or without a `p.wait()`.

Thanks,

Charlie


Answer: In the first variation, `test()` returns immediately after starting the
process, but before its output is sent to the console.

If you look at the output you _do_ get the prompt, immediately before the
output of `ls`.

    
    
    >>> test(['ls', '-l'])
    >>> total 0                            <----- test() returns, new propmpt
    --rw-r--r--    1 foo bar   7 Mar 24 17:38
    etc etc
    

In the second variation, `test()` is waiting for the process to terminate
before it returns.

In the third version, you're right that you may have to read from the child
process's `stdout` for it to continue.

