【问题标题】:Python secure websocket memory consumptionPython 安全 websocket 内存消耗
【发布时间】:2013-12-03 09:49:57
【问题描述】:

我正在用 python 编写一个 Web 套接字服务器。我用 txws、autobahn 和 tornado 尝试了下面的方法,结果都相似。

我似乎使用安全 websocket 消耗了大量内存,我无法弄清楚这可能发生在哪里或为什么会发生。下面是 tornado 中的示例,但我可以提供 autobahn 或 txws 中的示例。

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import json 

class AuthHandler(tornado.websocket.WebSocketHandler):

    def open(self):
        print 'new connection for auth'

    def on_message(self, message):
        message = json.loads(message)
        client_id = message['client_id']
        if client_id not in app.clients:
            app.clients[client_id] = self
        self.write_message('Agent Recorded')

    def on_close(self):
        print 'auth connection closed'


class MsgHandler(tornado.websocket.WebSocketHandler):

    def open(self):
        print 'new connection for msg'

    def on_message(self, message):
        message = json.loads(message)
        to_client = message['client_id']
        if to_client in app.clients:
            app.clients[to_client].write_message('You got a message')

    def on_close(self):
        print 'msg connection closed'


app = tornado.web.Application([
    (r'/auth', AuthHandler),
    (r'/msg', MsgHandler)
])

app.clients = {}


if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(app, ssl_options={
        'certfile': 'tests/keys/server.crt',
        'keyfile': 'tests/keys/server.key'
    })
    http_server.listen(8000)
    tornado.ioloop.IOLoop.instance().start()

在建立大约 10,000 个连接后,我发现我使用 SSL 时使用了大约 700MB 的内存,而没有使用 SSL 时使用的是 43MB,除非我终止该进程,否则我永远无法取回它。似乎问题与建立的连接数量而不是发送的消息密切相关。

消费似乎独立于客户端发生(我编写了自己的客户端并尝试了其他客户端)。

安全 websocket 真的比普通 websocket 占用更多内存吗?还是我的服务器代码没有正确实现它?

【问题讨论】:

标签: memory-leaks websocket twisted tornado autobahn


【解决方案1】:

我认为最好的解决方案是使用真正的网络服务器(nginx apache)作为代理,让它管理 ssl 层。

【讨论】:

  • 你为什么不相信 Python 网络服务器是“真实的”?
  • 触摸!!我的意思是真正意义上的模块,这些模块支持向世界开放的 http 服务器中需要的所有功能(日志记录、DoS 预防、负载平衡、编译,)
  • +1 for @JulienFr,Python 不应直接暴露于公共网络。 Apache / Nginx 是用较低级别的语言编写的,并针对它们拥有的单个任务进行了大量优化。
猜你喜欢
  • 2011-12-13
  • 1970-01-01
  • 2018-03-01
  • 2019-09-14
  • 2010-10-12
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2015-04-19
相关资源
最近更新 更多