GithubHelp home page GithubHelp logo

iamksm / adero Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 33 KB

This Python project demonstrates the usage of RabbitMQ for both Publish-Subscribe (PubSub) and Remote Procedure Call (RPC) communication patterns. RabbitMQ is a popular message broker that facilitates communication between different parts of a distributed application.

License: MIT License

Python 100.00%
messaging-library publisher-subscriber pubsub pubsub-messages

adero's Introduction

Adero

This Python project demonstrates the usage of RabbitMQ for both Publish-Subscribe (PubSub) and Remote Procedure Call (Request Response Usage) communication patterns. RabbitMQ is a popular message broker that facilitates communication between different parts of a distributed application.

Table of Contents

Introduction

This project demonstrates how to utilize RabbitMQ for both Publish-Subscribe and Remote Procedure Call communication patterns in Python. The Publish-Subscribe pattern allows one-to-many distribution of messages, while the Remote Procedure Call pattern allows invoking methods on a remote server.

Requirements

Getting Started

  1. Ensure you have RabbitMQ installed locally. You can follow the instructions here.

  2. Install the required dependencies using pip to your virtualenv

    pip install -e .
  3. Copy and paste the provided code snippets into separate terminals, adjusting configurations and queue/exchange names as needed.

    a. Create 2 new terminals and run Publisher and Subscriber (PubSub).

    b. Create 2 new terminals and run Server and Client (Request Response Usage).

Dependencies

PubSub Usage

Publisher

Publishes a message to a specified queue and exchange.

from adero.pubsub.publisher import Publisher

# Configure RabbitMQ connection
config = {
    "RABBIT_USER": "guest",
    "RABBIT_PASSWORD": "guest",
    "RABBIT_HOST_IP": "localhost",
    "RABBIT_PORT": 5672,
    "RABBIT_VHOST": "",
    "RABBIT_CONNECTION_TIMEOUT": 60 * 5,
    # has to be the same with what you will use in the subscriber
    "ENCRYPTION_KEY": b'b_xC4_-c3qo5TYmNhVO5MmtSbhutoLiHaxRomO1dszc='
}

# Create a Publisher instance
pub = Publisher("TEST-QUEUE", "TEST-EXCHANGE", config)

# Message to publish
msg = {
    "FirstName": "Kossam",
    "LastName": "Ouma",
    "Age": 18,
    "Children": ["First born", "Last born"]
}

# Publish the message
pub.publish(msg)

Subscriber

Receives and processes messages from the specified queue.

from adero.pubsub.subscriber import Subscriber

# Configure RabbitMQ connection
config = {
    "RABBIT_USER": "guest",
    "RABBIT_PASSWORD": "guest",
    "RABBIT_HOST_IP": "localhost",
    "RABBIT_PORT": 5672,
    "RABBIT_VHOST": "",
    "RABBIT_CONNECTION_TIMEOUT": 60 * 5,
    # has to be the same with what you used in the publisher
    "ENCRYPTION_KEY": b'b_xC4_-c3qo5TYmNhVO5MmtSbhutoLiHaxRomO1dszc='
}

# Processing function for received messages
def process_data(msg):
    # Your processing here
    my_data = msg["data"]
    message_properties = msg["properties"]  # is an instance of pika.BasicProperties

    print(f"My data - {my_data}, message properties - {message_properties}")

    # Return True if message was processed successfully
    successfully_processed = True
    return successfully_processed

# Create a Subscriber instance
sub = Subscriber("TEST-QUEUE", "TEST-EXCHANGE", config, process_data)

# Start consuming messages
sub.consume()

Request Response Usage

Server

Sets up a server to listen for Request Response Usage requests and processes them.

from adero.request_response.server import Server

def multiply_by_2(msg):
    number = msg.get("data")
    return number * 2

# Configure RabbitMQ connection
config = {
    "RABBIT_USER": "guest",
    "RABBIT_PASSWORD": "guest",
    "RABBIT_HOST_IP": "localhost",
    "RABBIT_PORT": 5672,
    "RABBIT_VHOST": "",
    "RABBIT_CONNECTION_TIMEOUT": 60 * 5,
    # has to be the same with what you used in the client
    "ENCRYPTION_KEY": b'b_xC4_-c3qo5TYmNhVO5MmtSbhutoLiHaxRomO1dszc='
}

# Create an Request Response Usage Server instance
server = Server("<YOUR_RPC_QUEUE>", "<YOUR_RPC_EXCHANGE>", config, multiply_by_2)

# Start listening for requests
server.listen()

Client

Sends Request Response Usage requests to the server and receives responses.

from adero.request_response.client import Client

# Configure RabbitMQ connection
config = {
    "RABBIT_USER": "guest",
    "RABBIT_PASSWORD": "guest",
    "RABBIT_HOST_IP": "localhost",
    "RABBIT_PORT": 5672,
    "RABBIT_VHOST": "",
    "RABBIT_CONNECTION_TIMEOUT": 60 * 5,
    # has to be the same with what you used in the server
    "ENCRYPTION_KEY": b'b_xC4_-c3qo5TYmNhVO5MmtSbhutoLiHaxRomO1dszc='
}

# Create an Request Response Usage Client instance
client_rpc = Client("<YOUR_RPC_QUEUE>", "<YOUR_RPC_EXCHANGE>", config)

# Send requests and receive responses
for number in range(1, 31):
    response = client_rpc.call(number)

    my_data = response["data"]
    message_properties = response["properties"]  # is an instance of pika.BasicProperties

    print(f"Response - {my_data}")
    print(f"message properties - {message_properties}\n")

Adding Concurrency

You can add concurrency using ThreadPoolExecutor to process multiple Request Response Usage requests concurrently.

import os
from collections import deque
from concurrent.futures import ThreadPoolExecutor

from adero.request_response.client import Client

# Configure RabbitMQ connection
config = {
    "RABBIT_USER": "guest",
    "RABBIT_PASSWORD": "guest",
    "RABBIT_HOST_IP": "localhost",
    "RABBIT_PORT": 5672,
    "RABBIT_VHOST": "",
    "RABBIT_CONNECTION_TIMEOUT": 60 * 5,
    # has to be the sametime with what you used in the publisher
    "ENCRYPTION_KEY": b'b_xC4_-c3qo5TYmNhVO5MmtSbhutoLiHaxRomO1dszc='
}

# Define the request processing function
def process_requests(i):
    client_rpc = Client("<YOUR_RPC_QUEUE>", "<YOUR_RPC_EXCHANGE>", config)
    print(f" [x] Requesting fib({i})")
    response = client_rpc.call(i)

    my_data = response["data"]
    message_properties = response["properties"]  # is an instance of pika.BasicProperties

    print(f"My data - {my_data}, message properties - {message_properties}")

# Set number of workers
cpu_count = os.cpu_count()
number_of_workers = cpu_count() // 2 or cpu_count

# Create a ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=number_of_workers) as t_exe:
    t_exe.map(process_requests, range(1, 31))

Note

To generate a valid ENCRYPTION_KEY you need to run the below and have that as an environment variable across the apps you will be using adero on.

from adero import generate_key

key = generate_key()
print(key)

Conclusion

This Python project demonstrates the usage of RabbitMQ for both Publish-Subscribe and Remote Procedure Call communication patterns. By following the provided examples, you can integrate RabbitMQ into your applications to achieve efficient and scalable communication between components.

adero's People

Contributors

iamksm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.