Package: gh-rabbit-hole
An advanced Python package to asynchronously manage RabbitMQ
connections, queues, and message handling using asyncio. Ideal for
high-performance, non-blocking applications.FeaturesAsynchronous Operations: Utilize the full potential of asyncio
for non-blocking message sending and receiving.Customizable Logging: Leverage colored logs for better monitoring
and debugging.Connection Resilience: Automatically reconnects in case of
connection failures.Message Handling: Send and receive messages asynchronously with
optional callbacks.Factory Design: Utilize the factory pattern for creating
instances with various configurations.InstallationInstall the package via pip:pipinstallasyncio-rabbit-managerUsageBasic SetupFirst, import the necessary classes:fromasyncio_rabbit_managerimportAsyncioRabbitManagerFactory,AsyncioRabbitManagerCreating a ManagerUse the factory to create an instance ofAsyncioRabbitManager:factory=AsyncioRabbitManagerFactory()rabbit_manager=factory.create('<config_file_name>')Sending MessagesSend messages to a specified queue:rabbit_manager.send_message('Hello, World!','my_queue')Receiving MessagesDefine a callback for incoming messages and start listening:asyncdefhandle_message(channel,method,properties,body):print("Received message:",body)rabbit_manager.on_message_callback=handle_messageawaitrabbit_manager.connect()Closing ConnectionProperly close the connection when done:awaitrabbit_manager.close_connection()Advanced UsageTheAsyncioRabbitManagerclass provides an asynchronous interface
for connecting and interacting with RabbitMQ using Python and asyncio.
Designed for applications requiring reactive and non-blocking
processing, this class manages the connection to RabbitMQ, sending and
receiving messages, and declaring exchanges and queues, all
asynchronously.FeaturesAsynchronous Connection:Establishes non-blocking connections to
RabbitMQ, allowing the rest of the application to continue executing
while managing network operations in the background.Channel Management:Opens and configures RabbitMQ channels to
send and receive messages.Message Sending and Receiving:Supports sending messages to
queues or exchanges and configuring asynchronous callbacks for
incoming message handling.Asyncio Integration:Built around asyncio primitives for easy
integration with other asynchronous operations and the event loop.Advanced Logging:Utilizes a customizable logging system to
monitor activities and quickly diagnose issues.UsageIdeal for asyncio-based applications that require efficient and
asynchronous communication with RabbitMQ. Especially useful in contexts
where performance and responsiveness are critical, such as in
microservices, bots, or real-time data processing systems.Initializationimportloggingfromyour_moduleimportAsyncioRabbitManager# Replace with the actual module namelogger=logging.getLogger(__name__)rabbit_manager=AsyncioRabbitManager(amqp_url="your_amqp_url",sending_queue="your_sending_queue",listening_queue="your_listening_queue",sending_exchange="your_sending_exchange",logger=logger,on_message_callback=your_message_callback_function# Replace with your callback)Connecting to RabbitMQimportasyncioasyncdefmain():awaitrabbit_manager.connect()asyncio.run(main())Sending Messagesmessage={"key":"value"}# Your message contentrouting_key="your_routing_key"# Optionalto_exchange=False# Set to True if sending to an exchangerabbit_manager.send_message(message,routing_key,to_exchange)Receiving MessagesImplement your message handling logic in a callback function:asyncdefmessage_handler(channel,method,properties,body):print("Received message:",body)# Set this as your callbackrabbit_manager.on_message_callback=message_handlerClosing the Connectionasyncdefclose():awaitrabbit_manager.close_connection()asyncio.run(close())ContributingContributions, issues, and feature requests are welcome!LicenseDistributed under the MIT License. SeeLICENSEfor more information.ContactMassimo Ghiani -m.ghiani@gmail.comProject Link:https://github.com/m-ghiani/RABBIT_HOLE