How to write to a named pipe?

James Logajan JamesL at Lugoj.Com
Mon Aug 16 23:33:01 EDT 1999


singlets at 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.




More information about the Python-list mailing list