Skip to content

Instantly share code, notes, and snippets.

@bitrut
Last active October 16, 2017 11:25
Show Gist options
  • Save bitrut/bc22224cd19bb06d404f8cf7059b5f6d to your computer and use it in GitHub Desktop.
Save bitrut/bc22224cd19bb06d404f8cf7059b5f6d to your computer and use it in GitHub Desktop.
aio-pika issue #71: Event loop is closed when exiting QueueIterator
import asyncio
import aio_pika
async def main(loop):
connection = await aio_pika.connect_robust("amqp://guest:guest@127.0.0.1/", 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()
import asyncio
import aio_pika
async def main(loop):
connection = await aio_pika.connect_robust("amqp://guest:guest@127.0.0.1/", 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