From 42d0228b438db95b4c82525537b85c4fec3dbd4b Mon Sep 17 00:00:00 2001 From: rapsealk Date: Mon, 5 Sep 2022 10:21:06 +0900 Subject: [PATCH 1/5] docs: Update redis to follow the latest spec --- docs/web_advanced.rst | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index 81cb2ca410b..0cda84ca512 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -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: - await sub.unsubscribe(ch.name) - await sub.quit() + import redis.asyncio as redis + from aiohttp import web + + + 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) + if msg is not None: + for ws in app['websockets']: + ws.send_str('{}: {}'.format(channel, msg)) + await asyncio.sleep(0.01) + except asyncio.CancelledError: + break + await pubsub.unsubscribe(channel) + await pubsub.close() async def background_tasks(app): From 5afc19cb4c6eddac3013f4fa3cc4f1db4380b529 Mon Sep 17 00:00:00 2001 From: rapsealk Date: Tue, 6 Sep 2022 10:31:28 +0900 Subject: [PATCH 2/5] Add news fragment --- CHANGES/6907.doc | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/6907.doc diff --git a/CHANGES/6907.doc b/CHANGES/6907.doc new file mode 100644 index 00000000000..d02725528d7 --- /dev/null +++ b/CHANGES/6907.doc @@ -0,0 +1 @@ +Updated Redis code examples from the document to follow the latest API. From 938f6af62a04dd95eba6316b4bb8be388a275b1f Mon Sep 17 00:00:00 2001 From: Jeongseok Kang Date: Thu, 8 Sep 2022 09:36:24 +0900 Subject: [PATCH 3/5] fix(docs): Simplify redis example using async context manager --- docs/web_advanced.rst | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index 0cda84ca512..d80930e172d 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -896,26 +896,19 @@ background tasks could be registered as an :attr:`Application.on_startup` signal handler or :attr:`Application.cleanup_ctx` as shown in the example below:: - import redis.asyncio as redis - from aiohttp import web - - 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) - if msg is not None: - for ws in app['websockets']: - ws.send_str('{}: {}'.format(channel, msg)) - await asyncio.sleep(0.01) - except asyncio.CancelledError: - break - await pubsub.unsubscribe(channel) - await pubsub.close() + async with client.pubsub() as pubsub: + await pubsub.subscribe(channel) + while True: + try: + msg = await pubsub.get_message(ignore_subscribe_messages=True) + if msg is not None: + for ws in app['websockets']: + ws.send_str('{}: {}'.format(channel, msg)) + except asyncio.CancelledError: + break async def background_tasks(app): From e735d061e668fa3e8df70dc9fc3ec150e2d12437 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Tue, 25 Oct 2022 20:11:24 +0100 Subject: [PATCH 4/5] Update docs/web_advanced.rst --- docs/web_advanced.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index d80930e172d..80fd8a46a99 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -906,7 +906,7 @@ below:: msg = await pubsub.get_message(ignore_subscribe_messages=True) if msg is not None: for ws in app['websockets']: - ws.send_str('{}: {}'.format(channel, msg)) + await ws.send_str('{}: {}'.format(channel, msg)) except asyncio.CancelledError: break From e610e4b64ed4547fe6847581fbe94e11b0d47b5a Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 30 Oct 2022 12:29:10 +0000 Subject: [PATCH 5/5] Update web_advanced.rst --- docs/web_advanced.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index 80fd8a46a99..220a4d8e7df 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -897,16 +897,16 @@ signal handler or :attr:`Application.cleanup_ctx` as shown in the example below:: async def listen_to_redis(app: web.Application): - client = redis.from_url('redis://localhost:6379') - channel = 'news' + client = redis.from_url("redis://localhost:6379") + channel = "news" async with client.pubsub() as pubsub: await pubsub.subscribe(channel) while True: try: msg = await pubsub.get_message(ignore_subscribe_messages=True) if msg is not None: - for ws in app['websockets']: - await ws.send_str('{}: {}'.format(channel, msg)) + for ws in app["websockets"]: + await ws.send_str("{}: {}".format(channel, msg)) except asyncio.CancelledError: break