A better popen2

Peter Astrand astrand at lysator.liu.se
Sat Jun 26 18:06:55 CEST 2004


On Fri, 25 Jun 2004 P at draigBrady.com wrote:

> > The most conspicuous recent one was (I think) called
> > popen5, and rumor has it, will be standard with 2.4,
> > and will have the select functionality you need, not
> > only on UNIX but also on Windows, where it isn't so
> > trivial a feat.
>
> Cool! I missed that: http://www.python.org/peps/pep-0324.html
> I had a 10 second look at process.Popen.communicate() and it seems
> to do much the same as I did, however it's missing a timeout
> parameter like I implemented which I think is important.

The current communicate() only supports a "read and write everything"
mode. communicate() won't return until all data is exchanged, and the
process is finished. In this case, I guess a timeout isn't very useful.

I've recently got in contact with Mark Pettit, which was interested in a
communicate-like method which could be called more than once. In this
case, a timeout is useful. We haven't decided on anything yet, but I've
thought about how to make this functionality available in communicate().
One idea is to add a few new keyword arguments, so that it would look
like:

def communicate(input=None, timeout=None,
                all=True, linemode=False):

If all is true, then it would behave just like before. If all is false, it
will return as soon as any stdout or stderr data is available, or the
timeout has gone off. Additionally, if linemode is true, then it will
return as soon as a complete line has been read, from stdout/stderr.

Comments? Should we make a separate method instead?

(I have a patch which implements the idea above, if anyone is interested.)

Btw, the PEP is not updated yet, so anyone interested in my process.py
should look at http://www.lysator.liu.se/~astrand/popen5/ instead.

/Peter Åstrand <astrand at lysator.liu.se>





More information about the Python-list mailing list