Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/6907.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated Redis code examples from the document to follow the latest API.
34 changes: 20 additions & 14 deletions docs/web_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -896,20 +896,26 @@ background tasks could be registered as an :attr:`Application.on_startup`
signal handler or :attr:`Application.cleanup_ctx` as shown in the example
below::


async def listen_to_redis(app):
try:
sub = await aioredis.create_redis(('localhost', 6379))
ch, *_ = await sub.subscribe('news')
async for msg in ch.iter(encoding='utf-8'):
# Forward message to all connected websockets:
for ws in app[websockets]:
ws.send_str('{}: {}'.format(ch.name, msg))
except asyncio.CancelledError:
pass
finally:
Comment thread
rapsealk marked this conversation as resolved.
await sub.unsubscribe(ch.name)
await sub.quit()
import redis.asyncio as redis
from aiohttp import web


Comment thread
rapsealk marked this conversation as resolved.
Outdated
async def listen_to_redis(app: web.Application):
client = redis.from_url('redis://localhost:6379')
pubsub = client.pubsub()
channel = 'news'
await pubsub.subscribe(channel)
while True:
try:
msg = await pubsub.get_message(ignore_subscribe_messages=True, timeout=1.0)
Comment thread
rapsealk marked this conversation as resolved.
Outdated
if msg is not None:
for ws in app['websockets']:
ws.send_str('{}: {}'.format(channel, msg))
await asyncio.sleep(0.01)
Comment thread
rapsealk marked this conversation as resolved.
Outdated
except asyncio.CancelledError:
break
await pubsub.unsubscribe(channel)
await pubsub.close()


async def background_tasks(app):
Expand Down