如何从子进程向Tornado中的websocket-client发送消息?

时间:2016-05-03 20:13:04

标签: python websocket tornado

我有Tornado服务器。它从websocket-connections接收消息。我需要将worker函数作为一个单独的进程运行,worker应该回答客户端。主要思想是以并行模式工作。像这样的东西

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

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

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

    def open(self):
        print("WebSocket opened")
        self.application.webSocketsPool.append(self)

    def on_message(self, message):
        for key, value in enumerate(self.application.webSocketsPool):
            if value == self:
                p = Process(target=worker, args=(value.ws_connection,message,))
                p.start()

    def on_close(self):
        print("WebSocket closed")
        for key, value in enumerate(self.application.webSocketsPool):
            if value == self:
                del self.application.webSocketsPool[key]

当然,由于酸洗错误,这不起作用。如何解决这个问题?

0 个答案:

没有答案