从另一个URL处理程序写入龙卷风websockets列表

问题描述:

我正在创建一个具有websocket处理程序的web应用程序。在每个成功的连接上,我将websocket处理程序对象附加到列表中。 另一个名为PostResultHandler的处理程序类接受POST数据。 PostResultHandler将由发送json数据的后台进程调用。一旦这个json数据被PostResultHandler接收到,我想写入websockets列表。从另一个URL处理程序写入龙卷风websockets列表

目前,我刚刚经历的WebSockets的列表迭代和JSON数据写入到它。但我认为这可能是一个阻塞调用,调用PostResultHandler的后台进程将被阻塞,直到结果写入所有websocket。

有什么办法,使这片代码非阻塞,使后台进程将继续无任何延迟

The Tornado examples folder includes a chat demo with websockets运行,它根本:

for waiter in cls.waiters: 
     try: 
      waiter.write_message(chat) 
     except: 
      logging.error("Error sending message", exc_info=True) 

这是不是一阻止呼叫。消息立即在服务器上缓存,并且代码继续执行。

最好的选择是在ioloop上添加一个回调来发送每个websocket处理程序实例的数据。你可以做下面的事情。

tornado.ioloop.IOloop.instance()。add_callback(部分(websocket_handler_instance.write_message,MSG))