相关疑难解决方法(0)

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

我正在使用以下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)

python stdin buffering

24
推荐指数
4
解决办法
2万
查看次数

实时subprocess.Popen通过stdout和PIPE

我试图stdout通过subprocess.Popen电话抓住,虽然我通过这样做很容易实现:

cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE)
for line in cmd.stdout.readlines():
    print line
Run Code Online (Sandbox Code Playgroud)

我想stdout"实时" 抓住.使用上面的方法,PIPE正在等待抓取所有stdout,然后它返回.

因此,对于记录目的,这不符合我的要求(例如,"看到"发生时发生的事情).

有没有办法一行一行地stdout运行?或者这是一个限制subprocess(必须等到PIPE关闭).

编辑 如果我转readlines()readline(),我只得到了最后一行stdout(不理想):

In [75]: cmd = Popen('ls -l', shell=True, stdout=PIPE)
In [76]: for i in cmd.stdout.readline(): print i
....: 
t
o
t
a
l

1
0
4
Run Code Online (Sandbox Code Playgroud)

python logging subprocess pipe popen

18
推荐指数
3
解决办法
4万
查看次数

标签 统计

python ×2

buffering ×1

logging ×1

pipe ×1

popen ×1

stdin ×1

subprocess ×1