推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
vissssa
V2EX  ›  Python

求助 tornado 写的 websocket cpu 占用 100%

  •  
  •   vissssa · Sep 18, 2019 · 3941 views
    This topic created in 2610 days ago, the information mentioned may be changed or developed.

    几个客户端一连接 cpu 就飙升到最高,且客户端断开也不降,
    求助代码有啥问题,或者有什么别的 websocket 方案么,
    下面是代码

    from abc import ABC
    
    from tornado import websocket, web, ioloop, httpserver, gen
    from tornado.options import define, options
    
    define("port", default=9040, help="run on the given port", type=int)
    
    class WebSocketHandler(websocket.WebSocketHandler, ABC):
    
        # CORS
        def check_origin(self, origin):
            return True
    
        def open(self):
            self.write_message({'code': 0, 'message': '连接成功'})
    
        async def on_message(self, message):
            user = self.get_argument('user', '')
            if not user:
                await self.write_message({'code': 411, 'message': '缺少用户 id'})
            else:
                while True:
                    try:
                        await self.write_message({'code': 0, 'user': user})
                        await gen.sleep(3)
                    except websocket.WebSocketClosedError:
                        self.on_close()
    
        def on_close(self):
            pass
    
    
    class HealthyCheckHandler(web.RequestHandler, ABC):
        def get(self):
            self.write("Hello, world")
    
    
    def make_app():
        return web.Application([
            ("/v1/ws/message", WebSocketHandler),
            ("/v1/ws/healthycheck", HealthyCheckHandler)
        ])
    
    
    def main():
        ws_app = make_app()
        options.parse_command_line()
        server = httpserver.HTTPServer(ws_app)
        server.listen(options.port)
        ioloop.IOLoop.current().start()
    
    
    if __name__ == '__main__':
        try:
            main()
        except KeyboardInterrupt:
            pass
        finally:
            ioloop.IOLoop.current().stop()
            ioloop.IOLoop.current().close(True)
    
    9 replies  •  2019-09-18 12:20:57 +08:00
    tsbx
    tsbx
    Sep 18, 2019
    1
    不知道 楼主 用的系统是什么呢?还有 tornado 的版本呢?
    robot1
    robot1
    Sep 18, 2019
    2
    twisted 啊 比 tornado 性能要高,易理解
    robot1
    robot1
    Sep 18, 2019
    3
    twisted + autobahn
    vissssa
    vissssa
    OP
    Sep 18, 2019
    4
    @robot1 #3 好的 我来试试
    linw1995
    linw1995
    Sep 18, 2019❤️1
    5
    except websocket.WebSocketClosedError:
    self.on_close()
    这个后面要补个 return 或者 break 啊
    vissssa
    vissssa
    OP
    Sep 18, 2019
    6
    @linw1995 #5 解决了 cpu 高的问题,但是为什么会这样呢,直接进了 WebSocketClosedError 中去了 这里加了 break 还需要 close()吗
    justfly
    justfly
    Sep 18, 2019
    7
    while 死循环了,跟框架没关系
    Hstar
    Hstar
    Sep 18, 2019
    8
    因为你的代码一直在 报错-捕捉-pass
    skymei
    skymei
    Sep 18, 2019
    9
    ```python
    while True:
    try:
    await self.write_message({'code': 0, 'user': user})
    await gen.sleep(3)
    except websocket.WebSocketClosedError:
    self.on_close()
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3515 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:07 · PVG 18:07 · LAX 02:07 · JFK 05:07
    ♥ Do have faith in what you're doing.