Last active
April 12, 2018 08:44
-
-
Save askabelin/786034c63ff8f4849c86690a9cbb8778 to your computer and use it in GitHub Desktop.
Multiple readers work asynchronously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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