ÿØÿà JFIF ` ` ÿþxØ
| Server IP : 109.234.164.53 / Your IP : 216.73.216.136 Web Server : Apache System : Linux cervelle.o2switch.net 4.18.0-553.32.1.lve.el8.x86_64 #1 SMP Thu Dec 19 13:14:03 UTC 2024 x86_64 User : computer3 ( 1098) PHP Version : 7.1.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /opt/alt/python35/share/doc/alt-python35-aioredis/examples/ |
Upload File : |
import asyncio
import aioredis
async def pubsub():
sub = await aioredis.create_redis(
'redis://localhost')
ch1, ch2 = await sub.subscribe('channel:1', 'channel:2')
assert isinstance(ch1, aioredis.Channel)
assert isinstance(ch2, aioredis.Channel)
async def async_reader(channel):
while await channel.wait_message():
msg = await channel.get(encoding='utf-8')
# ... process message ...
print("message in {}: {}".format(channel.name, msg))
tsk1 = asyncio.ensure_future(async_reader(ch1))
# Or alternatively:
async def async_reader2(channel):
while True:
msg = await channel.get(encoding='utf-8')
if msg is None:
break
# ... process message ...
print("message in {}: {}".format(channel.name, msg))
tsk2 = asyncio.ensure_future(async_reader2(ch2))
# Publish messages and terminate
pub = await aioredis.create_redis(
'redis://localhost')
while True:
channels = await pub.pubsub_channels('channel:*')
if len(channels) == 2:
break
for msg in ("Hello", ",", "world!"):
for ch in ('channel:1', 'channel:2'):
await pub.publish(ch, msg)
asyncio.get_event_loop().call_soon(pub.close)
asyncio.get_event_loop().call_soon(sub.close)
await asyncio.sleep(0)
await pub.wait_closed()
await sub.wait_closed()
await asyncio.gather(tsk1, tsk2)
if __name__ == '__main__':
import os
if 'redis_version:2.6' not in os.environ.get('REDIS_VERSION', ''):
loop = asyncio.get_event_loop()
loop.run_until_complete(pubsub())