【问题标题】:Python SSH Server( twisted.conch) takes up high cpu usage when a large number of echoPython SSH Server(twisted.conch) 大量echo时占用cpu高
【发布时间】:2016-11-16 16:41:41
【问题描述】:

我用 Twisted Conch 编写了一个 SSH 服务器。但是遇到了一个棘手的问题。假设用户A和用户B通过ssh命令登录到twisted ssh服务器。然后用户A在服务器上tail或cat一个大文件(大于100M),通过twisted ssh服务器会引起大量回显,使得python ssh进程(twisted.conch)cpu使用率很高(大于95%) ,甚至100%),那么用户B就会被屏蔽,很长时间没有反应。当发现用户A通过twisted ssh服务器有大量回声并且不阻塞其他连接的用户时,有没有办法让用户A的会话(0.5秒)休眠。

import sys  
import checkers  
from twisted.python import components, log, logfile  
from twisted.cred import portal  
from twisted.internet import reactor  
from twisted.conch.ssh import factory, keys, session, filetransfer  
from twisted.conch.unix import UnixSSHRealm, SSHSessionForUnixConchUser, UnixConchUser  
import keyvalue  
if __name__ == "__main__":  
   sshFactory = factory.SSHFactory()  
   sshFactory.portal = portal.Portal(UnixSSHRealm())  
   sshFactory.portal.registerChecker(checkers.UsernamePasswordChecker())  

   sshFactory.publicKeys = {
    'ssh-rsa': keys.Key.fromString(keyvalue.publicKey)}
   sshFactory.privateKeys = {
    'ssh-rsa': keys.Key.fromString(keyvalue.privateKey)}
   components.registerAdapter(
    SSHSessionForUnixConchUser, UnixConchUser, session.ISession)
   log.startLogging(sys.stdout)

   reactor.listenTCP(2222, sshFactory)
   reactor.run()

【问题讨论】:

    标签: python linux ssh twisted.conch


    【解决方案1】:

    这实际上是 Twisted 中的一个错误。使用服务器的一个用户不应该产生太多的负载,以至于它对其他所有人都没有响应。

    但是,要解决这个问题并不容易。有几种解决方案。

    首先,在您做任何其他事情之前,您应该确保您的代码使用PyPy,这可能会为您提供支持更多用户所需的所有额外性能。即使这还不够,也应该与这些其他解决方案结合使用。

    一个是您可以在多个进程中运行此代码,使用策略like this,这将允许您在多个内核上抢先运行该进程。当然,这并不能让你在一个进程中同时做很多事情。

    另一种选择是您可以使用twisted.protocols.htb(可以在sshFactory 上使用)来限制传入流量并确保在竞争连接之间公平处理。

    请分享您在这方面取得的任何进展,因为我相信其他 Twisted 用户会对此感兴趣!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2022-10-20
      • 2021-11-21
      • 2014-12-11
      • 1970-01-01
      相关资源
      最近更新 更多