Skip to content Skip to sidebar Skip to footer

How Do I Properly Write To Fifos In Python?

Something very strange is happening when I open FIFOs (named pipes) in Python for writing. Consider what happens when I try to open a FIFO for writing in a interactive interpreter:

Solution 1:

read() doesn't return until it reaches EOF.

You can try specifying the number of bytes you want read, like read(4). This will still block until enough bytes have been written, so the producer must write at least that many bytes and then call flush().

Solution 2:

To avoid the need for flushing, open the file without buffering:

fifo_read = open('fifo', 'r', 0)

That will remove high-level buffering. Data go to the OS directly and, being a fifo, they never get actually written to disk but passed straight to the reader thru the fifo buffer, so you don't need to sync.

Of course, you should have created the fifo first with os.mkfifo() or mkfifo at the shell, as you pointed in a comment.

Post a Comment for "How Do I Properly Write To Fifos In Python?"