我正在使用以下bash命令模式运行memcached:
memcached -vv 2>&1 | tee memkeywatch2010098.log 2>&1 | ~/bin/memtracer.py | tee memkeywatchCounts20100908.log
Run Code Online (Sandbox Code Playgroud)
尝试跟踪无与伦比的获取到平台键的集合.
memtracer脚本位于下方并按预期工作,只有一个小问题.看到中间日志文件大小,memtracer.py在memkeywatchYMD.log大小约为15-18K之前不会开始输入.有没有更好的方法来读取stdin或者可能是将缓冲区大小降低到1k以下以获得更快的响应时间?
#!/usr/bin/python
import sys
from collections import defaultdict
if __name__ == "__main__":
keys = defaultdict(int)
GET = 1
SET = 2
CLIENT = 1
SERVER = 2
#if <
for line in sys.stdin:
key = None
components = line.strip().split(" ")
#newConn = components[0][1:3]
direction = CLIENT if components[0].startswith("<") else SERVER
#if lastConn != newConn:
# lastConn = newConn
if direction == CLIENT:
command = SET if …Run Code Online (Sandbox Code Playgroud) 可能重复:
为sys.stdin设置较小的缓冲区大小?
我有一个Python(2.4/2.7)脚本fileinput用于从标准输入或文件中读取.它易于使用,除了一个案例外,效果很好:
tail -f log | filter.py
Run Code Online (Sandbox Code Playgroud)
问题是我的脚本缓冲了它的输入,而(至少在这种情况下)我希望立即看到它的输出.这似乎源于fileinput 在执行任何操作之前使用它readlines()来获取其bufsize值的字节这一事实.我尝试使用1的bufsize,它似乎没有帮助(这有点令人惊讶).
我确实发现我可以编写这样不缓冲的代码:
while 1:
line = sys.stdin.readline()
if not line: break
sys.stdout.write(line)
Run Code Online (Sandbox Code Playgroud)
这样做的问题是我丢失了fileinput功能(即它会自动打开传递给我程序的所有文件,如果没有则会自动打开stdin,它甚至可以自动解压缩输入文件).
那么我怎样才能充分利用两者呢?理想情况下,我不需要显式管理我的输入文件列表(包括解压缩),但在以"流"方式使用时不会延迟输入.