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.5

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.5
File Size Uploaded
pymbcap-0.0.5.tar.gz 880.4 kB Details

Built distribution (wheel)

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

Total release size: 887.5 kB

Release files / pymbcap-0.0.5.tar.gz

Download URL pymbcap-0.0.5.tar.gz
Size 880.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fd07a4fd35498449fa9bcef06c01cbca7581fa5f8d8fe4eed49df0a4090aed2e
BLAKE2b-256 checksum
How to use checksums
ac94c6dabb846a3c2c52b14f74dfa0bfe37565ef15700c604484515011dc2795
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.5-py3-none-any.whl

Download URL pymbcap-0.0.5-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9c2f9a230f0b8c21a341af2bd01a0af708967a2a5a81603a152253e18b253fb
BLAKE2b-256 checksum
How to use checksums
a1dc5573793c34fc0b5cdf796ad63f0843d7037a4828f09b831d1f7926985c1f
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

This release

0.0.5 This release

2 release files

0.0.4

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