From 2781b96e1dcb861559aa764c8ecdf728d037cabb Mon Sep 17 00:00:00 2001 From: Jeongseok Kang Date: Sun, 30 Oct 2022 21:52:01 +0900 Subject: [PATCH] docs: Update redis to follow the latest spec (#6907) ## 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 <Name> <Surname>. * Please keep alphabetical order, the file is sorted by names. - [x] Add a new news fragment into the `CHANGES` folder * name it `.` 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 (cherry picked from commit 0af182ae0ec3980b9164bf0b9ce9a3dc69b5b8ff) --- CHANGES/6907.doc | 1 + docs/web_advanced.rst | 27 +++++++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) 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. diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index 363442b608f..793d98229fb 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -892,20 +892,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:: - - 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() + async def listen_to_redis(app: web.Application): + 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)) + except asyncio.CancelledError: + break async def background_tasks(app):