问题
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