Python-为sys.stdin设置较小的缓冲区大小?

我正在使用以下bash命令模式运行memcached:

memcached -vv 2>&1 | tee memkeywatch2010098.log 2>&1 | ~/bin/memtracer.py | tee memkeywatchCounts20100908.log

尝试跟踪整个平台上关键的无与伦比的装备。

memtracer脚本在下面,并且可以按预期运行,但有一个小问题。注意中间日志文件的大小,直到memkeywatchYMD.log的大小约为15-18K时,memtracer.py才开始获取输入。有没有更好的方法可以读入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 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:

回答:

你可以使用python的-u标志从stdin / stdout完全删除缓冲:

-u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)

see man page for details on internal buffering relating to '-u'

手册页阐明了:

   -u     Force stdin, stdout and stderr to  be  totally  unbuffered.   On

systems where it matters, also put stdin, stdout and stderr in

binary mode. Note that there is internal buffering in xread-

lines(), readlines() and file-object iterators ("for line in

sys.stdin") which is not influenced by this option. To work

around this, you will want to use "sys.stdin.readline()" inside

a "while 1:" loop.

除此之外,不支持更改现有文件的缓冲,但是可以使用os.fdopen来创建一个新文件对象,该文件对象具有与现有文件相同的基础文件描述符,并且可能使用不同的缓冲。即

import os

import sys

newin = os.fdopen(sys.stdin.fileno(), 'r', 100)

应该绑定newin到读取与标准输入相同的FD的文件对象的名称,但是一次只能缓冲大约100个字节(并且你可以继续sys.stdin = newin从此开始使用新的文件对象作为标准输入)。我说“应该”,因为这方面的使用有一些平台上的一些错误和问题的(这是相当困难的功能,提供跨平台的全通用) -我不知道它的状态是什么了,但我” d绝对建议在所有感兴趣的平台上进行全面测试,以确保一切顺利。(-u,如果可能会满足你的要求,则完全删除缓冲在所有平台上应减少出现问题)。

以上是 Python-为sys.stdin设置较小的缓冲区大小? 的全部内容, 来源链接: utcz.com/qa/426581.html

回到顶部