summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Bull <aa6bs0@sambull.org>2022-10-30 00:49:54 +0100
committerGitHub <noreply@github.com>2022-10-30 01:49:54 +0200
commit8a5c47059fe126e22a79c63cfb168d035754049b (patch)
treeab11df8b7e2359a2fca0548eb6473558d09a7a89
parentbea00b16c8043397c6bb6f3df45d48bceefa685f (diff)
downloadredis-py-8a5c47059fe126e22a79c63cfb168d035754049b.tar.gz
Tidy up asyncio examples. (#2431)
-rw-r--r--docs/examples/asyncio_examples.ipynb68
1 files changed, 27 insertions, 41 deletions
diff --git a/docs/examples/asyncio_examples.ipynb b/docs/examples/asyncio_examples.ipynb
index 66d4358..dab7a96 100644
--- a/docs/examples/asyncio_examples.ipynb
+++ b/docs/examples/asyncio_examples.ipynb
@@ -140,8 +140,6 @@
"source": [
"import asyncio\n",
"\n",
- "import async_timeout\n",
- "\n",
"import redis.asyncio as redis\n",
"\n",
"STOPWORD = \"STOP\"\n",
@@ -149,29 +147,24 @@
"\n",
"async def reader(channel: redis.client.PubSub):\n",
" while True:\n",
- " try:\n",
- " async with async_timeout.timeout(1):\n",
- " message = await channel.get_message(ignore_subscribe_messages=True)\n",
- " if message is not None:\n",
- " print(f\"(Reader) Message Received: {message}\")\n",
- " if message[\"data\"].decode() == STOPWORD:\n",
- " print(\"(Reader) STOP\")\n",
- " break\n",
- " await asyncio.sleep(0.01)\n",
- " except asyncio.TimeoutError:\n",
- " pass\n",
+ " message = await channel.get_message(ignore_subscribe_messages=True)\n",
+ " if message is not None:\n",
+ " print(f\"(Reader) Message Received: {message}\")\n",
+ " if message[\"data\"].decode() == STOPWORD:\n",
+ " print(\"(Reader) STOP\")\n",
+ " break\n",
"\n",
"r = redis.from_url(\"redis://localhost\")\n",
- "pubsub = r.pubsub()\n",
- "await pubsub.subscribe(\"channel:1\", \"channel:2\")\n",
+ "async with r.pubsub() as pubsub:\n",
+ " await pubsub.subscribe(\"channel:1\", \"channel:2\")\n",
"\n",
- "future = asyncio.create_task(reader(pubsub))\n",
+ " future = asyncio.create_task(reader(pubsub))\n",
"\n",
- "await r.publish(\"channel:1\", \"Hello\")\n",
- "await r.publish(\"channel:2\", \"World\")\n",
- "await r.publish(\"channel:1\", STOPWORD)\n",
+ " await r.publish(\"channel:1\", \"Hello\")\n",
+ " await r.publish(\"channel:2\", \"World\")\n",
+ " await r.publish(\"channel:1\", STOPWORD)\n",
"\n",
- "await future"
+ " await future"
]
},
{
@@ -204,8 +197,6 @@
"source": [
"import asyncio\n",
"\n",
- "import async_timeout\n",
- "\n",
"import redis.asyncio as redis\n",
"\n",
"STOPWORD = \"STOP\"\n",
@@ -213,30 +204,25 @@
"\n",
"async def reader(channel: redis.client.PubSub):\n",
" while True:\n",
- " try:\n",
- " async with async_timeout.timeout(1):\n",
- " message = await channel.get_message(ignore_subscribe_messages=True)\n",
- " if message is not None:\n",
- " print(f\"(Reader) Message Received: {message}\")\n",
- " if message[\"data\"].decode() == STOPWORD:\n",
- " print(\"(Reader) STOP\")\n",
- " break\n",
- " await asyncio.sleep(0.01)\n",
- " except asyncio.TimeoutError:\n",
- " pass\n",
+ " message = await channel.get_message(ignore_subscribe_messages=True)\n",
+ " if message is not None:\n",
+ " print(f\"(Reader) Message Received: {message}\")\n",
+ " if message[\"data\"].decode() == STOPWORD:\n",
+ " print(\"(Reader) STOP\")\n",
+ " break\n",
"\n",
"\n",
"r = await redis.from_url(\"redis://localhost\")\n",
- "pubsub = r.pubsub()\n",
- "await pubsub.psubscribe(\"channel:*\")\n",
+ "async with r.pubsub() as pubsub:\n",
+ " await pubsub.psubscribe(\"channel:*\")\n",
"\n",
- "future = asyncio.create_task(reader(pubsub))\n",
+ " future = asyncio.create_task(reader(pubsub))\n",
"\n",
- "await r.publish(\"channel:1\", \"Hello\")\n",
- "await r.publish(\"channel:2\", \"World\")\n",
- "await r.publish(\"channel:1\", STOPWORD)\n",
+ " await r.publish(\"channel:1\", \"Hello\")\n",
+ " await r.publish(\"channel:2\", \"World\")\n",
+ " await r.publish(\"channel:1\", STOPWORD)\n",
"\n",
- "await future"
+ " await future"
]
},
{
@@ -298,4 +284,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
-} \ No newline at end of file
+}