简体   繁体   English

tornado.websocket和[Errno 24]太多打开的文件

[英]tornado.websocket and [Errno 24] Too many open files

I have got and error ( [Errno 24] Too many open files ) while testing tornado.websocket on a local machine. 在本地计算机上测试tornado.websocket时,我得到了错误( [Errno 24]打开文件太多 )。

server.py server.py

import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.options


class ChatSocketHandler(tornado.websocket.WebSocketHandler):
    waiters = set()

    def open(self):
        ChatSocketHandler.waiters.add(self)
        print "Clients: ", len(ChatSocketHandler.waiters)

    def on_close(self):
        ChatSocketHandler.waiters.remove(self)

    @classmethod
    def send_updates(cls, chat):
        for waiter in cls.waiters:
            try:
                waiter.write_message(chat)
            except:
                logging.error("Error sending message", exc_info=True)

    def on_message(self, message):
        ChatSocketHandler.send_updates(message)

app = tornado.web.Application([
    (r"/ws", ChatSocketHandler)
])

def main():
    tornado.options.parse_command_line()
    app.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

clients.py (using websocket-client ) clients.py (使用websocket-client

from multiprocessing import Pool, Process
from websocket import create_connection

def go():
    ws = create_connection("ws://127.0.0.1:8888/ws")

    while True:
        try:
            ws.send("Message ...")
            result =  ws.recv()
            print "Received '%s'" % result
        except KeyboardInterrupt:
            break
    ws.close()


for i in range(1000):
    Process(target=go).start()

The server dies after ~800 connections ;/ 服务器在~800个连接后死亡; /

Additional question: is it ok to set up a Nginx proxy to a tornado server instance? 附加问题:是否可以为龙卷风服务器实例设置Nginx代理? Do I get some benefits? 我能获得一些好处吗?

Your process likely runs out of file descriptors. 您的过程可能会耗尽文件描述符。 Here is a recipe for network tuning on Linux including how to increase max. 是Linux上网络调优的一个方法,包括如何增加最大值。 FDs. 函数依赖。 (This is for Crossbar.io , but will work for Tornado also). (这适用于Crossbar.io ,但也适用于Tornado)。

As to your question "does it make sense to put Nginx in front of Tornado": yes, definitely. 关于你的问题“将Nginx放在Tornado面前是否有意义”:肯定,是的。 Tornado's native TLS support is limited. Tornado的原生TLS支持有限。 Have a look at Hynek Schlawack: The Sorry State of SSL - PyCon 2014 看看Hynek Schlawack:SSL的抱歉状态 - PyCon 2014

Note: the latter does not apply to Twisted (or Crossbar.io, which is based on Twisted) - since Twisted uses pyOpenSSL and can be made to have high-quality TLS. 注意:后者不适用于Twisted(或Crossbar.io,它基于Twisted) - 因为Twisted使用pyOpenSSL并且可以使其具有高质量的TLS。 So there is no need for Nginx with these (at least not for TLS reasons). 所以Nginx不需要这些(至少不是因为TLS的原因)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM