我打开一个文件,读取文件,然后等待换行。有用。然后,文件旋转而不更改描述符(文件名保持不变)。我通过测量文件大小并将位置切换到0来检测到此事件。但是旧内容仍在内存中。有没有办法告诉python,当文件旋转时,您现在必须刷新缓冲区?谢谢
def readlines_then_tail(f, filename):
"""Iterate through lines and then wait for further lines"""
where = 0
while True:
f.seek(where)
line = f.readline()
if line:
yield line
where = f.tell()
else:
where = f.tell()
time.sleep(SLEEP_INTERVAL)
"""File can be rotated. Get it's size and if it is less than a current position start from the beginning"""
file_size = os.path.getsize(filename)
if int(where) > file_size:
where = 0