python script hangs when run from subprocess

Nobody nobody at nowhere.com
Sat Sep 7 11:47:47 EDT 2013


On Sat, 07 Sep 2013 03:55:02 -0700, Larry.Martell at gmail.com wrote:

> I have a python script and when I run it directly from the command line
> it runs to completion. But I need to run it from another script. I do
> that like this: 
> 
> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> rv = p.wait()
> out_buf = p.stdout.read()
> 
> When I do this, wait never returns.

The last two statements are the wrong way around. If you're reading a
process' output via a pipe, you shouldn't wait() for it until it has
closed its end of the pipe.

As it stands, you have a potential deadlock. If the subprocess tries to
write more data than will fit into the pipe, it will block until the
parent reads from the pipe. But the parent won't read from the pipe until
after the subprocess has terminated, which won't happen because the
subprocess is blocked waiting for the parent to read from the pipe ...





More information about the Python-list mailing list