A better popen2

Donn Cave donn at u.washington.edu
Fri Jun 25 14:06:21 EDT 2004


In article <40DC5219.6070907 at draigBrady.com>, P at draigBrady.com wrote:
> Donn Cave wrote:
...
> >   - dup2(a, sys.stdout.fileno()) -> dup2(a, 1)
> 
> doh! of course. I hate magic numbers
> but what I did was certainly wrong.

Hm, I feel like there's a lot of not-working stuff out
there because of someone's inability to get comfortable
with the UNIX system - file descriptors, ioctls, etc.
It's beautifully elegant to me.  Is an arbitrary name
any better than an arbitrary number?

> 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.

Well, I can't think of a time when I have needed it,
right off hand, but I guess when you need it, you need it.

Too bad he changed the name to "process", since from a
quick read it looks to me to support only command spawning,
not processes in general.

> I aggree that the appropriate interface is hard to define
> however I think we all agree that popen2 is definitely not it.

I don't know.  It has some problems that generate a
regular flow of questions on comp.lang.python, but it's
useful enough for the limited range of things that are
likely to work anyway.  If you want to solve a really
intractable pipe problem, see if you can get your stuff
working with what existing pty support there is in the
core distribution, for situations where the problem is
that the spawned command block-buffers its output when
talking to a pipe and the whole system deadlocks.

Where I've been motivated to write my own, the result
that I've actually used more than once has been a
sort of getstatusoutput() function, but raising an
exception instead of returning a status.  Like,

    try:
        text = cmdmodule.invoke(cmd)
    except cmdmodule.Error, value:
        print >> sys.stderr, repr(cmd[0]), 'aborted:', value
        sys.exit(1)

If I'm not mistaken, that's how we do things in Python,
we don't return status and expect you to check it.  But
you need select to do it (because the error value comes
from unit 2.)  Should be possible to build that on top
of your module, haven't tried it though.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list