Problem:
Program to read the lines from infinite stream starting from its end of file.
#Solution:
import time
def tail(theFile):
theFile.seek(0,2) # Go to the end of the file
while True:
line = theFile.readline()
if not line:
time.sleep(10) # Sleep briefly for 10sec
continue
yield line
if __name__ == '__main__':
fd = open('./file', 'r+')
for line in tail(fd):
print(line)
readline()
is a non-blocking read, with if check for every line.
Question:
It does not make sense for my program running to wait infinitely, after the process writing to file has close()
1) What would be the EAFP approach for this code, to avoid if
?
2) Can generator function return back on file
close?