named pipe question

Donn Cave donn at u.washington.edu
Tue Jul 13 14:23:02 EDT 2004


In article <pan.2004.07.13.17.17.40.237502 at presidency.com>,
 Rajarshi Guha <rajarshi at presidency.com> wrote:

>   I'm having a little trouble when I read from a named pipe. I create a
> pipe by
> 
> os.mkfifo('/tmp/mypipe')
> 
> and then open it for reading with
> 
> fin = open('/tmp/mypipe','r+')
> 
> (I don't use 'r' as that cause the open command to block). However when I
> do from another terminal:
> 
> ls -l /tmp/mypipe
> 
> and then from my Python session do:
> 
> fin.readlines()
> 
> it just hangs and I have to Ctrl-C to get out. But when I do from a
> terminal:
> 
> ls -l /tmp/mypipe ; cat < /tm/mypipe 
> 
> I get the output of the ls command. Why does'nt Python read from the pipe
> when some other program writes to it?

Well, there may be a couple of issues here.  The first is
that in order for anything to be read from a named pipe,
something has to be written to it.  That's elementary, but
neither of your examples suggest that this is happening.

Once you're writing to it, you will find that readlines()
will work only when the writer closes its pipe file descriptor
(possibly by exiting), because readlines() can't return
until the entire "file" has been read.

Don't use a named pipe if an ordinary disk file would do.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list