Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 带有WebSocket的Tornado协同程序不适用于python3_Python 3.x_Tornado - Fatal编程技术网

Python 3.x 带有WebSocket的Tornado协同程序不适用于python3

Python 3.x 带有WebSocket的Tornado协同程序不适用于python3,python-3.x,tornado,Python 3.x,Tornado,HandlerWebsockets工作正常,只是回复了当前通过messageToSockets(msg)发送的内容。但是,这两种尝试从web应用程序的协同程序向websocket发送消息的方法都不起作用。看起来一切都被这些尝试阻止了 class webApplication(tornado.web.Application): def __init__(self): handlers = [ (r'/', HandlerIndexPage),

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()
这里是WebSocket处理程序

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
定期发送消息,而不是“尽可能快”

generateMessageToSockets
将无休止地循环,以尽可能快的速度生成消息,而无需等待这些消息被发送。由于HTTPServer首先启动,而且从不屈服,因此它将永远无法实际接受连接

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

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

但最好使用
gen.sleep
定期发送消息,而不是“尽可能快”

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

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()

这很有效

不幸的是,所有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()

有效

谢谢你的回复。这只是一个简化的例子,像hello world这样的例子。生产代码只发送很少的更新。相应地更改了代码/过帐。不幸的是,仍然无法在命令行上看到任何打印或通过Routine生成的回复,最后我有了tp appologise,实际上您的建议在python 3.6.5和tornado 5.1上确实有效。。很抱歉,我必须将平台更改为oftenThanks以获得答复。这只是一个简化的例子,像hello world这样的例子。生产代码只发送很少的更新。相应地更改了代码/过帐。不幸的是,仍然无法在命令行上看到任何打印或通过Routine生成的回复,最后我有了tp appologise,实际上您的建议在python 3.6.5和tornado 5.1上确实有效。。很抱歉,我必须将该平台更改得远远超过那些后来看到它的人,他们使用的是Python 3.5.4/Tornado 4.5.1,而不是Python 3.6.5/Tornado 5.1…对于那些后来看到它的人,他们使用的是Python 3.5.4/Tornado 4.5.1,而不是Python 3.6.5/Tornado 5.1。。。