Skip to content

Commit 0af182a

Browse files
docs: Update redis to follow the latest spec (#6907)
<!-- Thank you for your contribution! --> ## What do these changes do? I changed some Redis code from the documentation to follow the latest API. It is based on redis-py>=4.2.x on which aioredis has been merged and started to be maintained. ## Are there changes in behavior for the user? Nope! ## Related issue number There is no issue related to this PR. ## Checklist - [x] I think the code is well written - [ ] Unit tests for the changes exist - [x] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` * The format is &lt;Name&gt; &lt;Surname&gt;. * Please keep alphabetical order, the file is sorted by names. - [x] Add a new news fragment into the `CHANGES` folder * name it `<issue_id>.<type>` for example (588.bugfix) * if you don't have an `issue_id` change it to the pr id after creating the pr * ensure type is one of the following: * `.feature`: Signifying a new feature. * `.bugfix`: Signifying a bug fix. * `.doc`: Signifying a documentation improvement. * `.removal`: Signifying a deprecation or removal of public API. * `.misc`: A ticket has been closed, but it is not of interest to users. * Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files." Thank you for reading and any feedback is always welcomed! Co-authored-by: Sam Bull <aa6bs0@sambull.org>
1 parent 18de88c commit 0af182a

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

CHANGES/6907.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated Redis code examples from the document to follow the latest API.

docs/web_advanced.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -896,20 +896,19 @@ background tasks could be registered as an :attr:`Application.on_startup`
896896
signal handler or :attr:`Application.cleanup_ctx` as shown in the example
897897
below::
898898

899-
900-
async def listen_to_redis(app):
901-
try:
902-
sub = await aioredis.create_redis(('localhost', 6379))
903-
ch, *_ = await sub.subscribe('news')
904-
async for msg in ch.iter(encoding='utf-8'):
905-
# Forward message to all connected websockets:
906-
for ws in app[websockets]:
907-
ws.send_str('{}: {}'.format(ch.name, msg))
908-
except asyncio.CancelledError:
909-
pass
910-
finally:
911-
await sub.unsubscribe(ch.name)
912-
await sub.quit()
899+
async def listen_to_redis(app: web.Application):
900+
client = redis.from_url("redis://localhost:6379")
901+
channel = "news"
902+
async with client.pubsub() as pubsub:
903+
await pubsub.subscribe(channel)
904+
while True:
905+
try:
906+
msg = await pubsub.get_message(ignore_subscribe_messages=True)
907+
if msg is not None:
908+
for ws in app["websockets"]:
909+
await ws.send_str("{}: {}".format(channel, msg))
910+
except asyncio.CancelledError:
911+
break
913912

914913

915914
async def background_tasks(app):

0 commit comments

Comments
 (0)