Fifo - reading in a loop

I want to use os.mkfifo for easy communication between programs. I have a problem reading from fifo in a loop.

Consider this toy example where I have a reader and writer working with fifo. I want to be able to run the reader in a loop to read everything that goes into fifo.

# reader.py import os import atexit FIFO = 'json.fifo' @atexit.register def cleanup(): try: os.unlink(FIFO) except: pass def main(): os.mkfifo(FIFO) with open(FIFO) as fifo: # for line in fifo: # closes after single reading # for line in fifo.readlines(): # closes after single reading while True: line = fifo.read() # will return empty lines (non-blocking) print repr(line) main() 

And the writer:

 # writer.py import sys FIFO = 'json.fifo' def main(): with open(FIFO, 'a') as fifo: fifo.write(sys.argv[1]) main() 

If I run python reader.py and later python writer.py foo , "foo" will be printed, but fifo will close and the reader will exit (or start inside the while ). I want the reader to stay in the loop, so I can write to the screenwriter many times.

Edit

I use this snippet to solve the problem:

 def read_fifo(filename): while True: with open(filename) as fifo: yield fifo.read() 

but maybe there is a more accurate way to handle this, instead of reopening the file ...

Similar

  • Getting readline to lock in FIFO
+7
python mkfifo
source share
2 answers

FIFO works (on the reader side) in this way: it can be read until all authors have disappeared. He then signals the EOF to the reader.

If you want the reader to continue reading, you will need to open it again and read from there. So your snippet is exactly what you need to do.

If you have several authors, you need to make sure that each piece of data written by them is smaller than PIPE_BUF so as not to mix messages.

+2
source share

You do not need to re-open the file again. You can use select to lock until data is available.

 with open(FIFO_PATH) as fifo: while True: select.select([fifo],[],[fifo]) data = fifo.read() do_work(data) 

In this example, you will not read EOF.

+1
source share

All Articles