Re: [Q]Two way pipes for UNIX child processes?

Tim Peters (tim@ksr.com)
2 Apr 1994 06:39:11 GMT

rmore@netcom.com (Rod Morehead) writes:
>...
>What I am trying to do is run a child process, feed it some input,
>see its response, feed it more input etc.
>...
>I wasn't able to spot anything that does this in the distribution. I
>know I can get a similar effect by staging files and redirecting input
>and output but I was hoping for a cleaner way.

Fellow Pythonites may kill me for this, but I suggest you zoom over to
comp.lang.perl and pick up the Perl FAQ. Not because Perl is necessarily
better at this than Python, but because people have been asking the same
question about Perl for years, and somebody already went to the trouble
of writing a good answer. Somebody already went to the trouble of doing
it the right way in Perl, too <wink>.

The short course is that pipes are an inherently poor base for what you
want to do, because they're prone to deadlock problems (e.g., what if
your child process simply doesn't respond when you thought it would?
then your program blocks forever waiting on a read that never yields).
Nevertheless, you _can_ give it a try, just like they explain in the Perl
FAQ (except rewrite it in Python <wink>).

Sockets are a better solution. The Perl FAQ will point you to some good
Perl packages for that, or e.g. you can take a look at Python's
nntplib.py (in the std library distribution) for a working example of how
to communicate in Python via sockets.

One friendly warning: UNIX(tm) does little to make any of this easy,
and vital details may differ depending on your system.

last-time-i-looked-at-this-stuff-i-ran-away-in-full-panic-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp