[Chicago] capturing output from subprocesses

Jess Balint jbalint at gmail.com
Wed Nov 16 22:13:46 CET 2005

On 11/16/05, Noel Thomas Taylor <nttaylor at uchicago.edu> wrote:
> I've tested it too and it works fine all around. This is the greatest.
> I'll be giving you big props in the final code.

Glad it's working.

> I had a few closing questions. At the beginning of the parent fork, you
> close the file descriptor for writing to stdErr, since you won't need it;
> meanwhile in the child fork you close the file descriptor for reading from
> stdErr, since you won't need it either. This all makes sense, but can't
> you also close the corresponding file descriptors for writing to and
> reading from stdOut, just as you did for stdErr? I added these "closes" to
> the code and noticed no ill effects, but thought I'd ask in case there was
> some reason I wasn't seeing.

If you don't close the other side of pipes, they won't return EOF (see
pipe(2) man page). The pty is a different story. You usually only open
the slave in the child, but since the openpty() does the whole thing,
we just take it as it is.

> Were you using your own function 'mygetpty()' on platforms / versions of
> Python where os.getpty() is not supported, like the Python 2.2 you had on
> your Solaris machine? I'm not sure about just when Python introduced this
> function, or if it is supported under Solaris. I don't have a Solaris
> machine here to test, and Python's own documentation pages are not very
> specific about which platforms support this function.

I just moved the mygetpty() code out to it's own function so I could
easily replace it with openpty() to see if it worked. As I said Python
2.2 (from Solaris companion CD) doesn't have openpty(), but
ActiveState 2.4 does. So you can make whatever changes you need in
that direction.

> Finally, I pretty much understand what's happening, but why do you have to
> make the calls to dup2? I've seen this in several process-spawning codes
> but have never figured out just why it's necessary.

dup2() is really the key to all this. You should read the man page for
details, but in a sentence it just redefines file descriptors. This
allows setting the new stdout and stderr for the child so it can be


More information about the Chicago mailing list