Skip to content

Tidy up asyncio examples. #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions docs/examples/asyncio_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,31 @@
"source": [
"import asyncio\n",
"\n",
"import async_timeout\n",
"\n",
"import redis.asyncio as redis\n",
"\n",
"STOPWORD = \"STOP\"\n",
"\n",
"\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"
]
},
{
Expand Down Expand Up @@ -204,39 +197,32 @@
"source": [
"import asyncio\n",
"\n",
"import async_timeout\n",
"\n",
"import redis.asyncio as redis\n",
"\n",
"STOPWORD = \"STOP\"\n",
"\n",
"\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"
]
},
{
Expand Down Expand Up @@ -298,4 +284,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}