Linux 如何在 Python 中正确写入 FIFO?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7048095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I properly write to FIFOs in Python?
提问by Thiago de Arruda
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:
当我在 Python 中打开 FIFO(命名管道)进行写入时,发生了一些非常奇怪的事情。考虑当我尝试打开 FIFO 以在交互式解释器中写入时会发生什么:
>>> fifo_write = open('fifo', 'w')
The above line blocks until I open another interpreter and type the following:
上面的行会阻塞,直到我打开另一个解释器并键入以下内容:
>>> fifo_read = open('fifo', 'r')
>>> fifo.read()
I don't understand why I had to wait for the pipe to be opened for reading, but lets skip that. The above code will block until there's data available as expected. However let's say I go back to the first interpreter window and type:
我不明白为什么我必须等待管道打开才能读取,但让我们跳过它。上面的代码将阻塞,直到有预期的数据可用。但是,假设我回到第一个解释器窗口并键入:
>>> fifo_write.write("some testing data\n")
>>> fifo_write.flush()
The expected behavior is that on the second interpreter the call to read
will return and we will see the data on the screen, except that is not happening to me. If I call os.fsync
the following happens:
预期的行为是在第二个解释器上调用read
将返回,我们将在屏幕上看到数据,但我没有发生这种情况。如果我打电话os.fsync
会发生以下情况:
>>> import os
>>> fifo_write.flush()
>>> os.fsync(fifo_write.fileno())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
And the fifo reader is still waiting. However, if I call fifo_writer.close()
then the data is flushed. If I use a shell command to feed the pipe:
而fifo阅读器仍在等待。但是,如果我调用,fifo_writer.close()
则数据将被刷新。如果我使用 shell 命令来馈送管道:
$ echo "some data" > fifo
then the reader output is:
那么阅读器的输出是:
>>> fifo_read.read()
'some data\n'
Has anyone experienced this? If so is there a workaround for it? My current OS is Ubuntu 11.04 with Linux 2.6.38.
有没有人经历过这个?如果是这样,是否有解决方法?我当前的操作系统是带有 Linux 2.6.38 的 Ubuntu 11.04。
采纳答案by Marcelo Cantos
read()
doesn't return until it reaches EOF.
read()
直到到达 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()
.
您可以尝试指定要读取的字节数,例如read(4)
. 这仍然会阻塞,直到写入足够的字节,因此生产者必须至少写入那么多字节,然后调用flush()
.
回答by 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.
当然,正如您在评论中指出的那样,您应该首先使用shellos.mkfifo()
或mkfifo
在 shell 上创建 fifo 。