Skip to content

Instantly share code, notes, and snippets.

@askabelin
Last active April 12, 2018 08:44
Show Gist options
  • Save askabelin/786034c63ff8f4849c86690a9cbb8778 to your computer and use it in GitHub Desktop.
Save askabelin/786034c63ff8f4849c86690a9cbb8778 to your computer and use it in GitHub Desktop.
Multiple readers work asynchronously
import asyncio
import aio_pika
async def read(reader_id):
connection = await aio_pika.connect_robust(**RMQ_CONFIG)
async with connection:
channel = await connection.channel()
queue = await channel.declare_queue('test_queue', durable=True)
message = await queue.get()
# the difference is here
while True:
try:
message = await queue.get()
print(reader_id)
await asyncio.sleep(1) # emulate async task
except Exception:
await asyncio.sleep(1)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
readers = [read(i) for i in range(3)]
loop.run_until_complete(asyncio.wait(readers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment