Skip to content

Instantly share code, notes, and snippets.

@askabelin
Last active April 11, 2018 16:19
Show Gist options
  • Save askabelin/6794590630590255283b979c2677c172 to your computer and use it in GitHub Desktop.
Save askabelin/6794590630590255283b979c2677c172 to your computer and use it in GitHub Desktop.
Multiple readers work synchronously
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
async for message in queue:
with message.process():
print(reader_id)
await asyncio.sleep(1) # emulate async task
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