Skip to main content

PyCAP

PyCAP, or PyMBCap (the MB is for message broker) is a simple package inspired by dotnetcore.CAP. Its purpose is to make implementing and working with a message broker e.g. RabbitMQ in Python, with a delay.

Right now, only RabbitMQ is implemented. Feel free to issue a pull request with another implementation. If you plan on doing so, please send me an email.

HOW TO SETUP

Setup Custom Container

This package requires a plugin for RabbitMQ if you want to use it with a delay. To set it up, this repository contains a folder named CustomRabbitMQContainer, that includes a Dockerfile, a docker-compose.yml, and 2 plugins. The container must be running for the package to work.

Wire the RabbitMQ Container for Dependency Injection

This package relies on dependency injection. This means that, for it to be injected, it must be first "added as a service". You can do so by following the example bellow:

from PyMBCap.Config.Config import wireRabbitMQContainer

if __name__ == "__main__":
    wireRabbitMQContainer([__name__])

    main()

*** You must also add the RABBITMQ_IP in your .env or environment, with the IP to the container. You can find this out by doing docker inspect [id of the container]. ***

Publish

To publish a message, simply establish a connection and fill in the required arguments, just like:

from PyMBCap.MessagerService.IMessageBrokerService import IMessageBrokerService
from dependency_injector.wiring import Provide, inject
from PyMBCap.Config.Container import RabbitMQContainer

@inject
def main(rabbitMQ : IMessageBrokerService = Provide[RabbitMQContainer.MessageService]):
    rabbitMQ.establish_connection()

    rabbitMQ.publish_function([exchange name], [routing_key], body = "Hello World", delay = 5000)

    print("Sent!")

In the example above, after filling in exchange name and queue name, there are also other optional arguments.

    def publish_function(self, exchange_name : str, routing_key : str, exchange_type : str = "x-delayed-message", delay : int = 0, durable : bool = True, body : str = ""):

Delay is in milliseconds.

Receive / Listen

To mark the function that is supposed to be triggered by the published message, use the decorator above the function with the same exchange_name and routing_key as you used to publish.

from PyMBCap.MessagerService.IMessageBrokerService import IMessageBrokerService
from dependency_injector.wiring import Provide, inject
from PyMBCap.Config.Container import RabbitMQContainer
from PyMBCap.MessagerService.Register import register

@inject
def main(rabbitMQ : IMessageBrokerService = Provide[RabbitMQContainer.MessageService]):
    @register(exchange_name=[exchange name], routing_key=[routing_key])
    def callback(ch, method, properties, body):
        print(f" [x] Received {body}")

In the example above, after filling in exchange name and queue name, there are also other optional arguments.

    def register_function(self, func, exchange_name : str, routing_key : str, exchange_type : str = "x-delayed-message", durable : bool = True, retryType : RetryTypes = RetryTypes.ALL, delayedRetryMin : int = 60000):

delayedRetryMin is in milliseconds.

Final words

If you need extra clarification, or want to implement a feature or expansion, don't hesitate to email me at gui.p.oliv@proton.me. You are also free to open an issue!

Metadata

Release files for PyMBCap 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyMBCap 0.0.4
File Size Uploaded
pymbcap-0.0.4.tar.gz 880.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyMBCap 0.0.4
File Interpreter ABI Platform
pymbcap-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 887.4 kB

Release files / pymbcap-0.0.4.tar.gz

Download URL pymbcap-0.0.4.tar.gz
Size 880.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9080684b809cca5bc733a45952641a3cb8088551cd426f28f2b5d4b9471a98c8
BLAKE2b-256 checksum
How to use checksums
c440924d8b6240aec3eab555287291c323108b112d0033ea0dbab97079973afc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / pymbcap-0.0.4-py3-none-any.whl

Download URL pymbcap-0.0.4-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3aba0e3054ac58c4f954970bd876c98ed83bd5c123b897418d8102df84acee9
BLAKE2b-256 checksum
How to use checksums
3441419f0cd0c68d5b7047ae5766c7c059eef634a013474c87b89af992a0eec5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

0.0.5

2 release files

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page