Python open a named pipe == hanging?

Donn Cave donn at u.washington.edu
Fri Aug 4 11:50:04 EDT 2006


In article <op.tdp5i6tsh12lye at cadet.mshome.net>,
 Rochester <rochester1976 at gmail.com> wrote:

>      I just found out that the general open file mechanism doesn't work
>      for named pipes (fifo).  Say I wrote something like this and it
>      simply hangs python:
> 
> #!/usr/bin/python
> 
> import os
> 
> os.mkfifo('my fifo')
> 
> open('my fifo', 'r+').write('some strings.')
> x = os.popen('cat my fifo').read()
> 
> print x

I believe your problem is that, by the time you open the
pipe for read, it has already been closed by its writer.
If you contrive to keep the file pointer around until after
the reader has opened it, then you can read some data from
it.  (You can't read "all" the data, though - since you still
have the file open, it has no end of file - so you can't
solve the problem exactly as stated above.)

And the odds are fair that when you get this working, you
will run into some other inconvenient behavior.  Named pipes
are a little tricky.

>      I know I could use a tempfile instead of a fifo in this very
>      simple case, I just want to know is there a standard way of
>      handling fifos withing python.  Especially the non-trivial case
>      when I want to call a diff like system program which takes two
>      files as input.  Say I have two python string objects A and B, I
>      want to call diff to see what is the different between those two
>      strings, and return the finding as a string obj C back to python.
>      This certainly can be done in this way:
> 
> open('tmpfile1', 'w').write(A)
> open('tmpfile2', 'w').write(B)
> C = os.popen('diff tmpfile1 tmpfile2').read()
> 
>      But that's kinda awkward isn't it? :-) The Bash way of doing this
>      would be (suppose A is the stdout of prog2, B is the stdout of
>      prog3):
> 
> diff <(prog2) <(prog3) > C
> 
> What is the best way of doing this in Python?

Version 1.  That's also how shell programmers do it, as far
as I know.  That bash thing is a neat gimmick, borrowed from
Plan 9's "rc", but not a standard shell feature and not needed
for conventional UNIX programming.  That's my opinion.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list