簡體   English   中英

從長壽命的FIFO讀取時,為什么只有readline()而不是read()或readlines()返回?

[英]Why does only readline(), and not read() or readlines(), return when reading from a long-lived FIFO?

好的,我在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()阻塞直到只能讀取一行 ,然后在一行的內容可用時立即返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM