Last active
October 16, 2017 11:25
-
-
Save bitrut/bc22224cd19bb06d404f8cf7059b5f6d to your computer and use it in GitHub Desktop.
aio-pika issue #71: Event loop is closed when exiting QueueIterator
This file contains hidden or 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 main(loop): | |
connection = await aio_pika.connect_robust("amqp://guest:[email protected]/", loop=loop) | |
queue_name = "test_queue2" | |
# Creating channel | |
channel = await connection.channel() # type: aio_pika.Channel | |
# Declaring queue | |
queue = await channel.declare_queue(queue_name, auto_delete=True) # type: aio_pika.Queue | |
exchange = await channel.declare_exchange('test_exchange') | |
await queue.bind(exchange, routing_key='key') | |
async for message in queue: | |
with message.process(): | |
print(message.body) | |
if message.body.decode() == 'STOP': | |
break | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main(loop)) | |
asyncio.wait(t for t in asyncio.Task.all_tasks() if not t.done()) | |
print('ALL DONE') | |
loop.close() |
This file contains hidden or 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 main(loop): | |
connection = await aio_pika.connect_robust("amqp://guest:[email protected]/", loop=loop) | |
queue_name = "test_queue2" | |
# Creating channel | |
channel = await connection.channel() # type: aio_pika.Channel | |
# Declaring queue | |
queue = await channel.declare_queue(queue_name, auto_delete=True) # type: aio_pika.Queue | |
exchange = await channel.declare_exchange('test_exchange') | |
await queue.bind(exchange, routing_key='key') | |
async for message in queue: | |
with message.process(): | |
print(message.body) | |
if message.body.decode() == 'STOP': | |
break | |
async def entrypoint(loop): | |
try: | |
await main(loop) | |
finally: | |
await asyncio.wait([t for t in asyncio.Task.all_tasks() if not t.done()], loop=loop) | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(entrypoint(loop)) | |
print('ALL DONE') | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment