Python open a named pipe == hanging?

Alex Martelli aleax at mac.com
Thu Aug 3 22:13:56 EDT 2006


Rochester <rochester1976 at gmail.com> wrote:

> Hi,
> 
>      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:

...just as it would "hang" any other language...!-).  Looks like you may
not be fully cognizant of the semantics of fifos -- understandably,
because the man pages on most systems are not too limpid on the subject.
But there's a good one, e.g., at
<http://opengroup.org/onlinepubs/007908799/xsh/open.html> , and I
selectively quote...:

"""
O_RDWR
Open for reading and writing. The result is undefined if this flag is
applied to a FIFO.
"""

(which means that your open with r+ _might_ cause the system to make
demons fly out of your nose, according to e.g.
<http://everything2.com/index.pl?node_id=922462> ...:-); so, _don't_ use
'r+' to open your fifo.  But, moreover...:

"""
O_NONBLOCK
When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is set:

An open() for reading only will return without delay. An open() for
writing only will return an error if no process currently has the file
open for reading.
If O_NONBLOCK is clear:

An open() for reading only will block the calling thread until a thread
opens the file for writing. An open() for writing only will block the
calling thread until a thread opens the file for reading.
"""

This last paragraph is the crucial one: the fundamental semantics of
FIFOs in Unix is for the writing and reading processes (or more modernly
"threads":-) to "rendezvous" around their respective calls to open (2),
blocking until both of them reach the open point.  The semantics in the
nonblocking case(s) are somewhat weird (to my way of thinking, but then,
my Unix background _is_ somewhat dated!-), but they wouldn't help you
anyway, it seems to me -- looks like you'd like drastically different
semantics (with neither open blocking, or just the reading one), but
Unix just doesn't really offer them...


Alex



More information about the Python-list mailing list