How to pass websocket to child process in Python Tornado?

馋奶兔 提交于 2019-12-24 02:49:13

问题


I have a Tornado-server with some Websockethandler. I want to make a pool of workers in order to start a worker as a child process and to pass websocket connection to the worker. After the worker has finished it should send an answer to the client.

def worker(message):
    inp_dict = json.loads(message)
    t = inp_dict["time"]
    time.sleep(t)
    return "Hello, World! "*int(t)

class WebSocket(tornado.websocket.WebSocketHandler):
    def check_origin(self, origin):
        return True

    def open(self):
        print("WebSocket opened")

    def on_message(self, message):    
        self.write_message(worker(message))

    def on_close(self):
        print("WebSocket closed")

How to do it?


回答1:


Passing connections between processes is not supported. Instead, leave the connections in the main process and use e.g. a multiprocessing.Queue to send simple objects (plain old data) back and forth to the child processes.



来源:https://stackoverflow.com/questions/36991106/how-to-pass-websocket-to-child-process-in-python-tornado

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!