为sys.stdin设置较小的缓冲区大小?
我正在使用以下bash命令模式运行memcached:
memcached -vv 2>&1 | tee memkeywatch2010098.log 2>&1 | ~/bin/memtracer.py | tee memkeywatchCounts20100908.log
尝试跟踪无与伦比的获取到平台键的集合。
memtracer脚本位于下方并按预期工作,只有一个小问题。看到中间日志文件大小,memtracer.py在memkeywatchYMD.log大小约为15-18K之前不会开始输入。有没有更好的方法来读取stdin或者可能是将缓冲区大小降低到1k以下以获得更快的响应时间?
#!/usr/bin/pythonimport sysfrom collections import defaultdictif __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 components[1] == "set" else GET key = components[2] if command == SET: keys[key] -= 1 elif direction == SERVER: command = components[1] if command == "sending": key = components[3] keys[key] += 1 if key != None: print "%s:%s" % ( key, keys[key], )
烙印99
相关分类