fifo - reading in a loop

后端 未结 2 1883
失恋的感觉
失恋的感觉 2021-02-20 18:52

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

Consider this toy example, where I have a reade

相关标签:
2条回答
  • 2021-02-20 19:30

    You do not need to reopen the file repeatedly. You can use select to block 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 won't read EOF.

    0 讨论(0)
  • 2021-02-20 19:32

    A FIFO works (on the reader side) exactly this way: it can be read from, until all writers are gone. Then it signals EOF to the reader.

    If you want the reader to continue reading, you'll have to open again and read from there. So your snippet is exactly the way to go.

    If you have mutliple writers, you'll have to ensure that each data portion written by them is smaller than PIPE_BUF on order not to mix up the messages.

    0 讨论(0)
提交回复
热议问题