Python deadlock using subprocess.popen and communicate

Chris Rebert clp2 at rebertia.com
Wed Sep 21 23:52:02 EDT 2011


On Wed, Sep 21, 2011 at 8:32 PM, Roy Smith <roy at panix.com> wrote:
<snip>
> My reading of the docs
> (http://docs.python.org/release/2.6.7/library/subprocess.html#popen-objec
> ts) says that Popen.poll() doesn't return a value, it sets the object's
> return code attribute, which you can then interrogate.

Popen.poll():
    Check if child process has terminated. Set **and return**
returncode attribute.
[Direct quote from the docs; emphasis added]

> More than that, your loop logic looks amazingly complicated for what's
> basically a simple thing.  I suggest something along the lines of:
>
>   # assuming process.returncode is initially None
>   while process.returncode is None:
>      out, err = process.communicate()
>      Log(out)
>
> I haven't tested that, but I think (from reading the docs) that's the
> right idea.

There is no point in communicate()-ing multiple times; communicate()
reads until EOF and only returns after subprocess termination!

Cheers,
Chris



More information about the Python-list mailing list