我建议看一下David Beazley的Python生成器技巧,尤其是
第5部分:处理无限数据 。它将
tail -f logfile实时处理与命令等效的Python 。
# follow.py## Follow a file like tail -f.import timedef follow(thefile): thefile.seek(0,2) while True: line = thefile.readline() if not line: time.sleep(0.1) continue yield lineif __name__ == '__main__': logfile = open("run/foo/access-log","r") loglines = follow(logfile) for line in loglines: print line,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)