python script hangs when run from subprocess

Larry.Martell at gmail.com Larry.Martell at gmail.com
Sat Sep 7 13:24:59 EDT 2013


On Saturday, September 7, 2013 9:47:47 AM UTC-6, Nobody wrote:
> 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 ...

Thanks. I reversed the order of the wait and read calls, and it no longer hangs. 



More information about the Python-list mailing list