多个龙卷风客户端同时连接到龙卷风服务器 [英] python - Multiple tornado clients simultaneously connecting to tornado server

查看:84
本文介绍了多个龙卷风客户端同时连接到龙卷风服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在这里提到的,我让我的Tornado客户端连续不断地循环收听我的Tornado服务器- http://tornadoweb.org/en/stable/websocket.html#client-side-support 。看起来像这样:

I have my Tornado client continuously listening to my Tornado server in a loop, as it is mentioned here - http://tornadoweb.org/en/stable/websocket.html#client-side-support. It looks like this:

import tornado.websocket
from tornado import gen

@gen.coroutine
def test():
    client = yield tornado.websocket.websocket_connect("ws://localhost:9999/ws")
    client.write_message("Hello")

    while True:
        msg = yield client.read_message()
        if msg is None:
            break
        print msg

    client.close()

if __name__ == "__main__":
    tornado.ioloop.IOLoop.instance().run_sync(test)

我无法让多个客户端实例连接到服务器。第二个客户端在连接到服务器之前总是等待第一个客户端进程结束。服务器设置如下,参考带有Tornado的Websocket:从外部访问将消息发送给客户端龙卷风-听取多个客户端同时通过websockets

I'm not able to get multiple instances of clients to connect to the server. The second client always waits for the first client process to end before it connects to the server. The server is set up as follows, with reference from Websockets with Tornado: Get access from the "outside" to send messages to clients and Tornado - Listen to multiple clients simultaneously over websockets.

class WSHandler(tornado.websocket.WebSocketHandler):

    clients = set()

    def open(self):
        print 'new connection'
        WSHandler.clients.add(self)

    def on_message(self, message):
         print 'message received %s' % message
         # process received message
         # pass it to a thread which updates a variable
         while True:
             output = updated_variable
             self.write_message(output)

    def on_close(self):
        print 'connection closed'
        WSHandler.clients.remove(self)

application = tornado.web.Application([(r'/ws', WSHandler),])

if __name__ == "__main__":
     http_server = tornado.httpserver.HTTPServer(application)
     http_server.listen(9999)
     tornado.ioloop.IOLoop.instance().start()

但这没有用-由于某种原因,即使是AF我已经成功建立了第一个连接,第二个连接却无法连接,也就是说,它甚至没有添加到客户端集中。

But this has not worked - for some reason even after I have made a successful first connection, the second connection just fails to connect i.e. it does not even get added to the clients set.

我最初以为 True 不会阻止服务器接收和处理更多的客户端,但是如果没有它,多个客户端可以连接。如何在不使用 while True 的情况下从内部线程发送回不断更新的信息?

I initially thought the while True would not block the server from receiving and handling more clients, but it does as without it multiple clients are able to connect. How can I send back continuously updated information from my internal thread without using the while True?

任何帮助都会

推荐答案

感谢您的回答@xyres!我可以通过在 on_message 方法中启动一个将处理和 while True 交给处理程序的线程来使其工作 WSHandler 类之外的函数。我相信这使该方法可以在Tornado的IOLoop之外运行,从而解除新连接的阻塞。

Thanks for your answer @xyres! I was able to get it to work by starting a thread in the on_message method that handed processing and the while True to a function outside the WSHandler class. I believe this allowed for the method to run outside of Tornado's IOLoop, unblocking new connections.

这是我的服务器现在的样子:

This is how my server looks now:

def on_message(self, message):
    print 'message received %s' % message
    sendThread = threading.Thread(target=send, args=(self, message))
    sendThread.start()

def send(client, msg):
    # process received msg
    # pass it to a thread which updates a variable
    while True:
        output = updated_variable
        client.write_message(output)

发送位置是在类外部定义的函数,它为我执行所需的计算,并在 True 内写回客户端。

Where send is a function defined outside the class which does the required computation for me and writes back to client inside thewhile True.

这篇关于多个龙卷风客户端同时连接到龙卷风服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆