Catalog PyPI
Back to packageRELEASE DETAILS

0.0.3

PyPI

aio-bunny

Asyncio compatible wraper around the aio-pika library for consumining messages.

Published17 Jul 2024
Direct dependencies14
Transitive dependenciesNot calculated

README

Version 0.0.3 documentation

aio-bunny

An asyncio compatible wrapper around aio-pika that brings the follwoing advantages:

  • Simplified API: Abstracts the complexity of defining queues and exchanges, making it easy to set up consumer/producers.
  • Graceful Consumer Shutdown: Provides an API to stop consumers without disrupting unacknowledged tasks, ensuring a smooth and safe shutdown process.
  • Idempotent Consumers: Offers a straightforward API for integrating with external databases (e.g., Redis, MySQL) to ensure idempotency. As recommended in the RabbitMQ documentation, consumers configured with manual acknowledgment and concerned about avoiding duplicate message processing should implement idempotency.
  • Prometheus Integration: Integrates with Prometheus to collect useful metrics for monitoring the state of consumers and producers. You can easily choose whether to enable metric collection based on your needs.

Installation

pip install aio-bunny

Usage examples

In the simplest form, you can do the following to spin up a consumer:

import asyncio

from aio_bunny import Bunny


async def main():
    bunny = Bunny("amqp://guest:guest@127.0.0.1/")

    @bunny.consumer(
        queue="myqueue",
        exchange="myexchange",
        routing_key="key")
    async def on_message(msg: bytes):
        await asyncio.sleep(1)
        print('received:', msg.decode())


    await bunny.start()


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(main())
    loop.run_forever()


To shutdown the consumers gracefully, all you need to do is to call the stop method on the Bunny instance.
This method will stop the consumer from accepting new messages from the broker, waits for all inflight tasks to finish and exits cleanly.\

So In the following example, we want to stop the application whenever SIGINT OR SIGTERM is received:

import asyncio
import signal

from aio_bunny import Bunny


async def main():
    bunny = Bunny("amqp://guest:guest@127.0.0.1/")

    async def shutdown(signal, loop):
        print(f"Received exit signal: {signal.name}")
        await bunny.stop()  # Stopping the consumers here
        loop.stop()

    loop = asyncio.get_event_loop()
    # Call the shutdown funciton on any of these signals.
    signals = (signal.SIGTERM, signal.SIGINT)
    for s in signals:
        loop.add_signal_handler(s, lambda s=s: asyncio.create_task(shutdown(s, loop)))

    @bunny.consumer(
        queue="myqueue",
        exchange="myexchange",
        routing_key="key")
    async def on_message(msg: bytes):
        await asyncio.sleep(1)
        print('received:', msg.decode())


    await bunny.start()


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(main())
    loop.run_forever()

Versioning

This software follows Semantic Versioning

Development

Setting up development environment

Clone the project:

git clone repo_url
cd aio-bunny

Create a virtualenv for aio-bunny:

python3 -m venv venv
source venv/bin/activate

Install the requirements:

pip install -e '.[develop]'

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 15

Native requirements
14 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
aio-pika
Environment
{
  "extras": "",
  "marker": null,
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
aio-pika<10,>=6.8.0runtime
aio-pika
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
aio-pika; extra == "develop"runtime
async-generator
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
async-generator; extra == "develop"runtime
coverage
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
coverage!=4.3; extra == "develop"runtime
coveralls
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
coveralls; extra == "develop"runtime
pylama
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
pylama; extra == "develop"runtime
pytest
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest; extra == "develop"runtime
pytest-asyncio
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest-asyncio; extra == "develop"runtime
pytest-cov
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
pytest-cov; extra == "develop"runtime
shortuuid
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
shortuuid; extra == "develop"runtime
nox
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
nox; extra == "develop"runtime
sphinx
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
sphinx; extra == "develop"runtime
sphinx-autobuild
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
sphinx-autobuild; extra == "develop"runtime
timeout-decorator
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
timeout-decorator; extra == "develop"runtime
tox
Environment
{
  "extras": "",
  "marker": "extra == \"develop\"",
  "requires_python": "<4,>=3.10",
  "evaluation": "NOT_EVALUATED"
}
tox>=2.4; extra == "develop"runtime

Distribution files

2 collected
aio_bunny-0.0.3-py3-none-any.whl

bdist_wheel · 6.62 KB · Python py3

Source file
aio_bunny-0.0.3.tar.gz

sdist · 5.87 KB · Python source

Source file

Repository

PENDING
itsmehdi97/aio-bunny

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 21 Oct, 11:42 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:3f8e88e14ec52c64d65ef66c0849f91f0228f7cd0e9d63beee8741a4bb5a35bd

Other collected versions

View all collected versions →