Skip to content

Commit

Permalink
docs: Update redis to follow the latest spec (#6907)
Browse files Browse the repository at this point in the history
<!-- 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>
(cherry picked from commit 0af182a)
  • Loading branch information
rapsealk authored and patchback[bot] committed Oct 30, 2022
1 parent 6e9ecc7 commit 2781b96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/6907.doc
@@ -0,0 +1 @@
Updated Redis code examples from the document to follow the latest API.
27 changes: 13 additions & 14 deletions docs/web_advanced.rst
Expand Up @@ -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):
Expand Down

0 comments on commit 2781b96

Please sign in to comment.