Skip to content

Commit 0047a5b

Browse files
[PR #6907/0af182ae backport][3.9] docs: Update redis to follow the latest spec (#7059)
**This is a backport of PR #6907 as merged into master (0af182a).** <!-- 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: Jeongseok Kang <jskang@lablup.com>
1 parent 6e9ecc7 commit 0047a5b

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
@@ -892,20 +892,19 @@ background tasks could be registered as an :attr:`Application.on_startup`
892892
signal handler or :attr:`Application.cleanup_ctx` as shown in the example
893893
below::
894894

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

910909

911910
async def background_tasks(app):

0 commit comments

Comments
 (0)