How do I properly write to FIFOs in Python?

◇◆丶佛笑我妖孽 提交于 2019-11-29 03:12:32

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().

Mike

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!