推荐学习书目
› 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 · 3854 views
    This topic created in 2563 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
        1
    tsbx  
       Sep 18, 2019
    不知道 楼主 用的系统是什么呢?还有 tornado 的版本呢?
    robot1
        2
    robot1  
       Sep 18, 2019
    twisted 啊 比 tornado 性能要高,易理解
    robot1
        3
    robot1  
       Sep 18, 2019
    twisted + autobahn
    vissssa
        4
    vissssa  
    OP
       Sep 18, 2019
    @robot1 #3 好的 我来试试
    linw1995
        5
    linw1995  
       Sep 18, 2019   ❤️ 1
    except websocket.WebSocketClosedError:
    self.on_close()
    这个后面要补个 return 或者 break 啊
    vissssa
        6
    vissssa  
    OP
       Sep 18, 2019
    @linw1995 #5 解决了 cpu 高的问题,但是为什么会这样呢,直接进了 WebSocketClosedError 中去了 这里加了 break 还需要 close()吗
    justfly
        7
    justfly  
       Sep 18, 2019
    while 死循环了,跟框架没关系
    Hstar
        8
    Hstar  
       Sep 18, 2019
    因为你的代码一直在 报错-捕捉-pass
    skymei
        9
    skymei  
       Sep 18, 2019
    ```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   ·   901 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:30 · PVG 03:30 · LAX 12:30 · JFK 15:30
    ♥ Do have faith in what you're doing.