named pipe question

Donn Cave donn at
Tue Jul 13 20:23:02 CEST 2004

In article <pan.2004. at>,
 Rajarshi Guha <rajarshi at> 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

More information about the Python-list mailing list