简体   繁体   中英

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

OK, I have a FIFO file in Ubuntu.

with open(fifo_path) as f:
    while True:
        d = f.read()
        print(repr(d)) ## this is never called

This doesn't work, I never get any data, it just blocks indefinitely even when there is data.

with open(fifo_path) as f:
    while True:
        d = f.readlines()
        print(repr(d)) ## this is also never called

This doesn't work either.

with open(fifo_path) as f:
    while True:
        d = f.readline()
        print(repr(d)) ## only this is invoked

Only this works. I get the data and it keep reading each line forever.

Any idea why?

read() and readlines() both read the file's entire contents , and only return after that content has been read in full. If the write end of your FIFO is never closed, the file's contents are open-ended, so these calls can never return.

readline() , by contrast, blocks until it can read only one line , and returns as soon as that one line's content is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM