使用readline()轮询文件的更改在macOS上有效,但在Debian 9上无效



我想"轮询";用于更改的文件。

以下代码适用于macOS 10.15.7和Python 3.8.6,但不适用于Debian 9和Python 3.7.3。在Debian上,当我向要合并的文件添加新行时,它不会被识别,并一直打印支票。。

from time import sleep
from datetime import datetime

wait = 1
with open('file.log') as fp:
exit_pooling = False
while not exit_pooling:
print("*** check " + str(datetime.now()))
line = fp.readline()
if not line:
sleep(wait)
continue
else:
print('process line')

谢谢!

在Ubuntu18.04使用Python3.6:按预期工作

#!/usr/bin/env python3
from time import sleep
from datetime import datetime
from pathlib import Path
wait = 1
with Path('file.log').open() as fp:
exit_pooling = False
while not exit_pooling:
print("*** check " + str(datetime.now()))
line = fp.readline()
if not line:
sleep(wait)
else:
print(f'process line: {line}')
exit_pooling = line.strip() == 'exit'

相关内容