pipe problems

Werner Schiendl ws-news at gmx.at
Fri Mar 9 05:55:27 EST 2001


Maybe to much decoration in the last post...

readline() is a blocking function for pipes, ttys and so on.
If you readline() at the end of a file, it immediately returns, because the
file will (normally) not grow later.

But a pipe (like a tty) is filled from one side (a tty is 'filled' by a user
typing on the keyboard) and emptied from your application.
If there is currently nothing that can be read in the buffer, readline waits
for such data to become available

-> it blocks

This behaviour is to ease the use on one hand (you do not need to poll in a
loop, but just do one call and have the result - or know that there will not
be one) and to make multitasking more efficient on the other hand (it is of
no use to have your programm polling for data that the IO system knows much
better when it arrives).

I guess the programm you call does not print (a whole) line of text after
you output your command to it and the whole thing stops.

Does this clearify the situation?

regards
werner
Leonardo B Lopes <leo at iems.nwu.edu> wrote in message
news:3AA7DD16.93C0730D at iems.nwu.edu...
> Werner Schiendl wrote:
> >
> > If there is no data to read, readline it will block for such to become
> > available.
> > I don't think that cout.flush() is of any use, btw.
>
> When I tried the program with 'cat', it only worked if I used
> cout.flush(). But maybe it doesn't work in general. My guess is that
> this call is the crux of the problem.
>
> > Maybe I'm wrong, but I think data can only be flushed out by the sender
of a
> > pipe...
> > Further, what's if the program prints out something in between, or more
than
> > 1 line?
>
> That is something I'll deal with at a later point, when I get the basic
> stuff working. I have the impression there is something about pipes that
> I don't understand correctly.
>
> Thanks,
> Leo.
>
> >
> > Maybe you can do the reading in a seperate thread.
> > Alternatively you can try to find out if there _is_ sth you can read.
> >
> > hth
> > werner
> >
> > Leonardo B Lopes <leo at iems.nwu.edu> wrote in message
> > news:3AA6D886.4DD8D3A4 at iems.nwu.edu...
> > > I use a program that doesn't have readline compiled in. So I figured,
it
> > > would be easy to add a python wrapper around it. So I wrote the
wrapper,
> > > using 'cat' as a test command. With 'cat' it works fine. But when I
try
> > > using another program, such as 'python', the program just hangs at
> > > readline(). Anybody know how to accomplish this? Here is the code:
> > >
> > > #!/usr/local/bin/python
> > >
> > > import sys,os
> > > import cmd
> > >
> > > class Amplcmd(cmd.Cmd):
> > >
> > >         def __init__(self):
> > >                 cmd.Cmd.__init__(self)
> > >                 self.cin,self.cout =
os.popen2('/usr/local/bin/python')
> > >
> > >         def default(self,line):
> > >                 self.cin.write(line+'\n')
> > >                 self.cin.flush()
> > >                 self.cout.flush()
> > >                 print self.cout.readline(),
> > >
> > >         def do_EOF(self,line):
> > >                 sys.exit()
> > >
> > > mycmd = Amplcmd()
> > > mycmd.cmdloop()
> > >
> > >
> > >
> > > --
> > >
=======================================================================
> > > Leonardo B. Lopes
leo at iems.nwu.edu
> > > Ph.D. Student
(847)491-8470
> > > IEMS - Northwestern University
http://www.iems.nwu.edu/~leo
>
> --
> =======================================================================
> Leonardo B. Lopes                                      leo at iems.nwu.edu
> Ph.D. Student                                             (847)491-8470
> IEMS - Northwestern University             http://www.iems.nwu.edu/~leo





More information about the Python-list mailing list