python - 没有时间的python中的tail -f.sleep

标签 python

我需要在 python 中模拟“tail -f”,但我不想在阅读循环中使用 time.sleep。我想要一些更优雅的东西,比如某种阻塞读取,或者 select.select 超时,但是 python 2.6“选择”文档特别说:“它不能用于常规文件来确定文件自上次读取以来是否已经增长。 " 还有什么办法吗? 几天后,如果没有给出解决方案,我将阅读 tail 的 C 源代码以尝试解决。我希望他们不要使用 sleep ,呵呵 谢谢。

马里奥

最佳答案

(更新) 要么使用 FS 监控工具

或单次 sleep 使用(我认为您认为更优雅)。

import time
def follow(thefile):
    thefile.seek(0,2)      # Go to the end of the file
    while True:
         line = thefile.readline()
         if not line:
             time.sleep(0.1)    # Sleep briefly
             continue
         yield line

logfile = open("access-log")
loglines = follow(logfile)
for line in loglines:
    print line

关于python - 没有时间的python中的tail -f.sleep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1475950/

相关文章:

Python Tkinter - 选项数据库仅部分工作

Python函数导入文件名和城市名称

python - 将 NDArray 写入 JSON 和 .CV 文件

python - matplotlib 绘图集 x_ticks

Python/Numba : Unknown attribute error with scipy. special.gammainc()

Python - 拟合二维矩形数据

Python selenium : selenium. common.exceptions.NoSuchWindowException:消息:浏览上下文已被丢弃

python - 在警告被抑制后控制警告的出现

python - Tensorflow conv2d_transpose(deconv)out_backprop的行数与计算的不匹配

python - 请求方法值错误的 Django Rest Framework : `get_serializer_class` called several times,