带有Paramiko的ssh上的tail f具有越来越大的延迟

2024-07-01 07:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在运行的嵌入式系统的日志文件中检查错误。在

我已经在我的脚本中实现了paramiko,因为有人告诉我这是在python中使用ssh的最佳方式。在

现在当我跟踪日志文件时,我发现有一个很大的延迟累积。每分钟增加30秒。在

我已经使用grep来减少打印的行数,因为我认为我接收的输入太多了,但事实并非如此。在

如何在运行时减少此延迟或阻止延迟增加。我想跟踪几个小时。。。在

def mkssh_conn(addr):
    """returns an sshconnection"""
    paramiko.util.logging.getLogger('paramiko').setLevel(logging.WARN)
    sshcon = paramiko.SSHClient()
    sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    sshcon.connect(addr , username, password)
    return sshcon

while True:
    BUF_SIZE = 1024
    client = mkssh_conn() #returns a paramiko.SSHClient()
    transport = client.get_transport()
    transport.set_keepalive(1)
    channel = transport.open_session()
    channel.settimeout(delta)
    channel.exec_command( 'killall tail')
    channel = transport.open_session()
    channel.settimeout(delta)
    cmd = "tail -f /log/log.log | grep -E 'error|statistics'"
    channel.exec_command(cmd)
    while transport.is_active():
        print "transport is active"
        rl, wl, xl = select.select([channel], [], [], 0.0)
        if len(rl) > 0:
            buf = channel.recv(BUF_SIZE)
            if len(buf) > 0:
                lines_to_process = LeftOver + buf
                EOL = lines_to_process.rfind("\n")
                if EOL != len(lines_to_process)-1:
                    LeftOver = lines_to_process[EOL+1:]
                    lines_to_process = lines_to_process[:EOL]
                else:
                    LeftOver = ""
                for line in lines_to_process.splitlines():
                    if "error" in line:
                        report_error(line)
                    print line
    client.close()          

Tags: toclientlogparamikoleniflinechannel
2条回答

我找到了一个解决方案: 似乎如果我把BUF_的大小降低到256,延迟就会减少。显然。 我需要重新检查在运行时延迟是否仍然增加。在

如果您使用的是高吞吐量尾管,那么缓冲区的大小应该更高,以减少cpu周期(进而减少由于网络延迟而导致的总延迟)。在

进一步使缓冲区的大小变大,不应降低性能。(如果paramiko在低吞吐量管道中的缓冲区填满时才嚎啕大哭)

@studioj的回答与此相矛盾,可能是由于paramiko的升级(现已修复)

相关问题 更多 >

    热门问题