Skip to main content

Consumer/producer like library built over amqp (aioamqp)

Project description

info:

Consumer/producer like library built over amqp (aioamqp)

https://img.shields.io/travis/aio-libs/aioamqp_consumer.svg https://img.shields.io/pypi/v/aioamqp_consumer.svg

Installation

pip install aioamqp_consumer

Usage

import asyncio
from functools import partial

from aioamqp_consumer import Consumer, Producer


async def task(payload, options, sleep=0, *, loop):
    await asyncio.sleep(sleep, loop=loop)
    print(payload)


async def main(*, loop):
    amqp_url = 'amqp://guest:guest@127.0.0.1:5672//'
    amqp_queue = 'your-queue-here'
    queue_kwargs = {
        'durable': True,
    }
    amqp_kwargs = {}  # https://aioamqp.readthedocs.io/en/latest/api.html#aioamqp.connect

    async with Producer(amqp_url, amqp_kwargs=amqp_kwargs, loop=loop) as producer:
        for _ in range(5):
            await producer.publish(
                b'hello',
                amqp_queue,
                queue_kwargs=queue_kwargs,
            )

    consumer = Consumer(
        amqp_url,
        partial(task, loop=loop, sleep=1),
        amqp_queue,
        queue_kwargs=queue_kwargs,
        amqp_kwargs=amqp_kwargs,
        loop=loop,
    )
    await consumer.scale(20)  # scale up to 20 background coroutines
    await consumer.scale(5)  # downscale to 5 background coroutines
    await consumer.join()  # wait for rabbitmq queue is empty and all local messages are processed
    consumer.close()
    await consumer.wait_closed()

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop=loop))
loop.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioamqp_consumer-0.1.3.tar.gz (7.8 kB view details)

Uploaded Source

File details

Details for the file aioamqp_consumer-0.1.3.tar.gz.

File metadata

  • Download URL: aioamqp_consumer-0.1.3.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for aioamqp_consumer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8061be66eb870561df5f2639b43b975fe50f645e01ebcb16bc14eb171b0cd6f2
MD5 7e0795dc65e296c3c89caa72181d5598
BLAKE2b-256 f91784cf1e39b7287065d5c95e0bd50c0f75f5069bcc5da27421194ea1d7fceb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page