首页 > 解决方案 > sys.stdin 如何在 Python 中缓冲?

问题描述

我正在开发一个接受来自 Apache 的日志输出的脚本。我的解决方案如下:

while True:
        log_line = sys.stdin.readline()

        while(log_line == ""): # wait for new line 
            time.sleep(2)
            log_line = sys.stdin.readline() # buffer issue? 

        # process the line here 

问题是,在观察 apache 日志和我的脚本处理的行日志之间的差异时,我注意到我的程序缺少一些行。我希望这一定与在时间延迟期间被丢弃/覆盖的行有关,但我无法找到有关 Python 的 std.in 如何缓冲的深入信息。

我的假设是,如果在 time.sleep(2) 期间将多行发送到 std.in,它们将被缓冲并最终通过后续的 sys.stdin.readline() 调用访问。这不正确吗?

标签: pythonbufferstdin

解决方案


推荐阅读