async def main(): amqp_url = 'amqp://*****:*****@127.0.0.1:5672//' amqp_queue = 'your-queue-here' queue_kwargs = { 'durable': True, } # https://aioamqp.readthedocs.io/en/latest/api.html#aioamqp.connect amqp_kwargs = {} async with Producer(amqp_url, amqp_kwargs=amqp_kwargs) as producer: for _ in range(5): await producer.publish( b'hello', amqp_queue, queue_kwargs=queue_kwargs, ) consumer = Consumer( amqp_url, task, amqp_queue, queue_kwargs=queue_kwargs, amqp_kwargs=amqp_kwargs, ) await consumer.scale(20) # scale up to 20 background coroutines await consumer.scale(5) # downscale to 5 background coroutines # wait for rabbitmq queue is empty and all local messages are processed await consumer.join() consumer.close() await consumer.wait_closed()
async def listen(self): url = settings.AMQP_URL amqp_kwargs = {} queue_kwargs = {"durable": True} self.consumer = Consumer( url, self.handle, self.queue, queue_kwargs=queue_kwargs, amqp_kwargs=amqp_kwargs, loop=self.app.loop, concurrency=self.CONCURRENCY, ) await self.consumer.scale(20)
class QueueListener: CONCURRENCY = 1 def __init__(self, app, handler_cls, queue, shared_data=None): self.app = app self.queue = queue self.handler = handler_cls(self.app, shared_data=shared_data) self.consumer = None async def listen(self): url = settings.AMQP_URL amqp_kwargs = {} queue_kwargs = {"durable": True} self.consumer = Consumer( url, self.handle, self.queue, queue_kwargs=queue_kwargs, amqp_kwargs=amqp_kwargs, loop=self.app.loop, concurrency=self.CONCURRENCY, ) await self.consumer.scale(20) async def close(self): try: self.consumer.close() except BaseException as exc: logger.exception(exc, exc_info=exc) try: await self.consumer.join() except BaseException as exc: logger.exception(exc, exc_info=exc) try: await self.consumer.wait_closed() except BaseException as exc: logger.exception(exc, exc_info=exc) async def handle(self, *args, **kwargs): await self.handler.handle(*args, **kwargs)
async def wrapper(task, amqp_queue_name): consumer = Consumer( amqp_url, task, amqp_queue_name, ) await consumer.ok() return consumer
async def test_consumer_smoke(producer, loop, amqp_queue_name, amqp_url): test_data = [b'test'] * 5 for data in test_data: await producer.publish(data, amqp_queue_name) test_results = [] async def task(payload, options): test_results.append(payload) consumer = Consumer( amqp_url, task, amqp_queue_name, loop=loop, ) await consumer.join() consumer.close() await consumer.wait_closed() assert test_results == test_data