带有 websockets 的 Tornado 协程不能与 python3 一起工作

Tornado coroutine with websockets not working with python3

HandlerWebsockets 确实工作正常,只是回复目前通过 messageToSockets(msg) 发送的内容。但是,两者都尝试从 Web 应用程序的协程向 websocket 发送消息,但均无效。看起来一切都被这些尝试阻止了...

class webApplication(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', HandlerIndexPage),
            (r'/websocket', HandlerWebSocket, dict(msg='start')),
        ]

        settings = {
            'template_path': 'templates'
        }
        tornado.web.Application.__init__(self, handlers, **settings)

    @gen.coroutine
    def generateMessageToSockets(self):
        while True:
            msg = str(randint(0, 100))
            print ('new messageToCon: ', msg)
            yield [con.write_message(msg) for con in HandlerWebSocket.connections]
            yield gen.sleep(1.0)

if __name__ == '__main__':

    ws_app = webApplication()
    server = tornado.httpserver.HTTPServer(ws_app)
    port = 9090
    print('Listening on port:' + str(port))
    server.listen(port)
    IOLoop.current().spawn_callback(webApplication.generateMessageToSockets)
    IOLoop.current().set_blocking_log_threshold(0.5)
    IOLoop.instance().start()

这里是 WebSockets 处理程序

class HandlerWebSocket(tornado.websocket.WebSocketHandler):
    connections = set()

    def initialize(self, msg):
        print('HWS:' + msg)

    def messageToSockets(self, msg):
        print ('return message: ', msg)
        [con.write_message(msg) for con in self.connections]

    def open(self):
            self.connections.add(self)
            print ('new connection was opened')
            pass

    def on_message(self, message):
            print ('from WebSocket: ', message)
            self.messageToSockets(message)

    def on_close(self):
            self.connections.remove(self)
            print ('connection closed')
            pass

我有点迷失在示例、此处的问题、文档等中。因此,非常感谢任何有关如何正确启动连续调用 websocket 例程的提示

generateMessageToSockets 将无限循环,尽可能快地生成消息,而无需等待这些消息被发送。由于它首先启动并且从不让步,因此 HTTPServer 实际上永远无法接受连接。

如果您真的想尽可能快地发送消息,不阻塞的最小解决方案是

yield [con.write_message(msg) for con in HandlerWebSocket.connections]
yield gen.moment

但使用 gen.sleep 定期发送消息可能比 "as fast as possible" 更好。

不幸的是,所有 gen.routines 尝试都不适合我。移回线程

def generateMessageToSockets():
    while True:
        msg = str(randint(0, 100))
        print ('new messageToCon: ', msg)
        [con.write_message(msg) for con in HandlerWebSocket.connections]
        sleep(1.0)


class WebApplication(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', HandlerIndexPage),
            (r'/websocket', HandlerWebSocket, dict(msg='start')),
        ]

        settings = {
            'template_path': 'templates'
        }
        tornado.web.Application.__init__(self, handlers, **settings)

if __name__ == '__main__':
    tGenarate =  threading.Thread(target=generateMessageToSockets)
    tGenarate.start()
    ws_app = WebApplication()
    server = tornado.httpserver.HTTPServer(ws_app)
    port = 9090
    print('Listening on port:' + str(port))
    server.listen(port)
    ioloop.IOLoop.instance().start()

有效