Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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 当我在on_message tornado.websocket中遇到阻塞时,该怎么办?_Python_Websocket_Tornado - Fatal编程技术网

Python 当我在on_message tornado.websocket中遇到阻塞时,该怎么办?

Python 当我在on_message tornado.websocket中遇到阻塞时,该怎么办?,python,websocket,tornado,Python,Websocket,Tornado,我是tornado的新手。我正在尝试使用tornado构建聊天服务器代理,我从web客户端获得消息,通常它只需要将其发送回,但是,我需要先将这些消息发送到另一台服务器,问题来了,等待另一台服务器响应需要花费大量时间,我需要使其不被阻塞,但是当我使用龙卷风的匿名方法时,它根本不起作用,帮帮我,非常感谢 这是我的伪代码的一部分: class ClientWSConnectienter(websocket.WebSocketHandler): _thread_pool = ThreadPoolExe

我是tornado的新手。我正在尝试使用tornado构建聊天服务器代理,我从web客户端获得消息,通常它只需要将其发送回,但是,我需要先将这些消息发送到另一台服务器,问题来了,等待另一台服务器响应需要花费大量时间,我需要使其不被阻塞,但是当我使用龙卷风的匿名方法时,它根本不起作用,帮帮我,非常感谢

这是我的伪代码的一部分:

class ClientWSConnectienter(websocket.WebSocketHandler):

_thread_pool = ThreadPoolExecutor(20)

def initialize(self, room_handler):
    #chat room initiate
    self.__rh = room_handler

@run_on_executor(executor='_thread_pool')
def worker(self,msg):
    #send the msg to another server
    pmessage=send_msg_to_server(msg) 
    return pmessage

@tornado.web.asynchronous
@tornado.gen.coroutine
def on_message(self, message):
    #this will blocking for too much time,and I want make it no-blocking
    pmessage=yeild worker(msg) 
    #send the recive pmessage to others client
    room.write_message(pmessage) 
    self.finish()
显然,它不起作用,我得到了这样的结果:

error:websocket cannot use this method
class MyHandler(WebSocketHandler):
    def initialize(self):
        self.queue = tornado.queues.Queue()

    def on_open(self):
        IOLoop.current().spawn_callback(self.loop)

    def one_message(self, msg):
        self.queue.put(msg)

    def on_connection_close(self):
        self.queue.put(None)

    @coroutine
    def loop(self):
        while True:
            msg = yield self.queue.get()
            if msg is None:
                return
            pmessage = yield self.worker(msg)
            self.write_message(pmessage)
那么,我该怎么办?非常感谢

但在我重新编辑代码后,它仍然会阻塞任务部分。我不知道为什么,这仍然是我代码的一部分 重新编辑:


我认为错误消息来自您对
finish()
,这对WebSocket没有意义(您的意思是
close()
?)。(另外,不需要同时使用
@asynchronous
@coroutine
@coroutine
就足够了)

但还有一个更大的问题:请记住,当重写在超类中定义的方法时,只有在文档中说可以的情况下,才可以将它们设置为协同路由(因为协同路由的调用与常规方法不同)<代码>WebSocketHandler.on_消息目前(从Tornado 4.3开始)不支持协同路由

因此,您需要使用队列将此任务移交给另一个任务。大概是这样的:

error:websocket cannot use this method
class MyHandler(WebSocketHandler):
    def initialize(self):
        self.queue = tornado.queues.Queue()

    def on_open(self):
        IOLoop.current().spawn_callback(self.loop)

    def one_message(self, msg):
        self.queue.put(msg)

    def on_connection_close(self):
        self.queue.put(None)

    @coroutine
    def loop(self):
        while True:
            msg = yield self.queue.get()
            if msg is None:
                return
            pmessage = yield self.worker(msg)
            self.write_message(pmessage)

非常感谢你,大师,我很高兴得到你的回答,我的意思是,你的回答太棒了。我已经更改了我的代码,但仍然没有解决这个问题,再次得到你的回答会很好。问题中的新代码重新编辑,再次感谢你非常感谢