pipe related question

Donn Cave donn at u.washington.edu
Wed Nov 23 13:22:24 EST 2005


In article <mailman.984.1132625792.18701.python-list at python.org>,
 David Reed <dreedmac at columbus.rr.com> wrote:
> Is there any way to have one program run another arbitrary program  
> with input from stdin and display the output as if you had run it in  
> a shell (i.e., you'd see some of the output followed by the input  
> they typed in and then a newline because they pressed return followed  
> by subsequent output, etc.).
> 
> I can't use readline with the pipe because I don't know how much  
> output the arbitrary program has before it calls an input statement.  
> I've googled and understand that calling read() will deadlock when  
> the program is waiting for input.
> 
> When I first write all the input to the input pipe and then call read  
> on the output pipe it works just the same as if I had run the program  
> as: program < input_file
> 
> What I'd like to see is the input intermixed with the output as if  
> the user had typed it in.

It sounds like there may be two problems here.  You may need to
go to some extra lengths to get the arbitrary program to adopt
a convenient buffering strategy.  I'm sure you have come across
plenty of discussion of this problem in your review of the old
traffic in this group, since it comes up all the time.  The usual
answer is to use a pseudotty device instead of a regular pipe.
I don't know what's currently popular for Python support of this
device, but there's builtin support on some platforms, cf.
os.openpty and os.forkpty.  It may be convenient in your case
to turn ECHO on.

The other problem is more intractable.  If you want to know
for sure when the arbitrary program expects input, well, UNIX
doesn't support that.  (Can you run your application on VMS?)
The only thing I can think of is a select() with timeout, with
some compromise value that will allow most outputs to complete
without stalling longer than is really convenient.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list