几个客户端一连接 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)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.