Brukva library and toredis already has not been updated for several years. But the Tornado in the latest update finally switched to using the event queue asyncio, which gives the opportunity to use fresh and a good library. For example aioredis :
Естественно, это максимально упрощённый пример, в реальном коде соединения не стоит держать в глобальной переменной, а при завершении работы сервера стоит отписаться от канала и закрыть соединение с redis.
import asyncio
import aioredis
from tornado import web, websocket
from tornado.ioloop import IOLoop
connections = []
class WSHandler(websocket.WebSocketHandler):
def open(self):
connections.append(self)
def on_message(self, message):
...
def on_close(self):
connections.remove(self)
class GetHandler(web.RequestHandler):
def get(self):
self.render("chat.html")
async def consumer(channel):
while await channel.wait_message():
msg = await channel.get(encoding='utf-8')
for connection in connections:
await connection.write_message(msg)
async def setup():
connection = await aioredis.create_redis('redis://localhost')
channel = await connection.subscribe('notifications')
asyncio.ensure_future(consumer(channel))
application = web.Application([
(r'/', GetHandler),
(r'/chat/', WSHandler),
])
if __name__ == '__main__':
application.listen(8000)
loop = IOLoop.current()
loop.add_callback(setup)
loop.start()
Естественно, это максимально упрощённый пример, в реальном коде соединения не стоит держать в глобальной переменной, а при завершении работы сервера стоит отписаться от канала и закрыть соединение с redis.