Mailing List Archive

How to write to a named pipe?
Could someone help me with a basic thing? I have
an external application that reads from a named
pipe and then processes the information from said
pipe. I'd like to send this info from my python
app. I can create the named pipe with mkfifo()
just fine. Reading the docs, I gather that this
only creates the pipe and I must then treat the
pipe as a file. But when I go to access the "file"
(really the pipe), python just hangs. Really
hangs, i.e., no control-c or control-d stopping.
Here is my simple ditty:

%: python
Python 1.5.1 (#1, Mar 21 1999, 22:49:36) [GCC
egcs-2.91.66 19990314/Li on linux-i386
Copyright 1991-1995 Stichting Mathematisch
Centrum, Amsterdam
>>> import os
>>> os.mkfifo('/tmp/myfifo')
>>> f=open('/tmp/myfifo','w') # now we're stuck

I've checked permissions, should be ok. I've also
tried making the pipe with world rw permissions
from a prompt, then accessing this pipe from
python in the same manner as above. Same result.
No errors raised, so I assume it's trying to
connect to the pipe. As you can see, I'm running
python 1.5.1 on a stock RedHat 6 linux Intel box.
Sorry if I'm being an idiot.

Regards,
Steve



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
How to write to a named pipe? [ In reply to ]
singlets@my-deja.com wrote:
> >>> import os
> >>> os.mkfifo('/tmp/myfifo')
> >>> f=open('/tmp/myfifo','w') # now we're stuck

Try:

f = open('/tmp/myfifo', 'w+')

(Note the extra '+')

Not sure why 'rw' doesn't work. But 'r+' also works. Here is something you
can try: in one window start a Python interpreter and do:

f = open('/tmp/myfifo', 'w') # The usual block.

And then in the other interpreter do:

f = open('/tmp/myfifo', 'r') # Both should now return!

You can reverse the open order and get the same result. Basically pipes are
unidirectional and expect a reader and a writer. I'm so used to opening
pipes in 'C' in read/write mode that I've forgotten why things are like
this. At least it doesn't block, once you remember to always open read/write
or similar.
How to write to a named pipe? [ In reply to ]
singlets@my-deja.com writes:
[snip]
> %: python
> Python 1.5.1 (#1, Mar 21 1999, 22:49:36) [.GCC
> egcs-2.91.66 19990314/Li on linux-i386
> Copyright 1991-1995 Stichting Mathematisch
> Centrum, Amsterdam
>>>> import os
>>>> os.mkfifo('/tmp/myfifo')
>>>> f=open('/tmp/myfifo','w') # now we're stuck
[snip]

It is normal that Python hangs when you try to open the fifo for
writing, because the underlying C sys-call hangs waiting for some
process to open the pipe for reading.

The problem is that when Python hangs, it cannot receive signals; I
had the very same problem with the threading API, and the only
solution I found was to do polling.

Does anyone known a better solution?

> Sorry if I'm being an idiot.
>

I'm sorry too ;)

Bye,

--
Diego | To reply remove the `x' and swap the | Sorry for my
Dainese | words around the `@' in the address. | bad English!