好吧,我在ubuntu中有一个fifo文件。
with open(fifo_path) as f:
while True:
d = f.read()
print(repr(d)) ## this is never called
这是行不通的,我永远不会得到任何数据,即使有数据,它也只能无限期地阻止。
with open(fifo_path) as f:
while True:
d = f.readlines()
print(repr(d)) ## this is also never called
这也不起作用。
with open(fifo_path) as f:
while True:
d = f.readline()
print(repr(d)) ## only this is invoked
只有这起作用。我获取数据,并且它会永远阅读每一行。
任何想法为什么?
read()
和 readlines()
都读取文件的整个内容,并且仅在该内容后才返回。如果您的FIFO的写入末端从未关闭,则文件的内容是开放式的,因此这些呼叫永远无法返回。
readline()
可以阻止只能读取一行,并在该行的内容可用后立即返回。