https://github.com/projectweekend/pika-pack
Handy components when working with RabbitMQ and Pika (https://pika.readthedocs.org/en/latest/)
https://github.com/projectweekend/pika-pack
Last synced: over 1 year ago
JSON representation
Handy components when working with RabbitMQ and Pika (https://pika.readthedocs.org/en/latest/)
- Host: GitHub
- URL: https://github.com/projectweekend/pika-pack
- Owner: projectweekend
- License: mit
- Created: 2015-02-15T19:44:34.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-02T23:27:20.000Z (about 11 years ago)
- Last Synced: 2025-03-05T14:07:36.278Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 219 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.txt
- License: LICENSE
Awesome Lists containing this project
README
This is a small collection of things that I've found useful working with [RabbitMQ](http://www.rabbitmq.com/) using the [Pika](https://pika.readthedocs.org/en/latest/) library.
### RPCBlockingConsumer
This class connects to RabbitMQ, binds an `exchange` and `routing_key`, and listens for new messages. The `request_action` parameter is a function that itself receives a single parameter of the incoming message body as a dictionary. This function is used to perform any custom operations, and should return another dictionary. The resulting dictionary becomes the message body that is sent back to the sender via the `reply_to` queue. The `reconnect_attempts` parameter is the number of times you would like the listener to attempt reconnection to RabbitMQ should the connection be lost after it starts to consume messages.
Once an instance of `RPCBlockingConsumer` is created, call the `start` method to begin processing messages. This is a blocking operation.
**Example:**
```python
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
ROUTING_KEY = 'name_of_routing_key'
RECONNECT_ATTEMPTS = 3
def my_custom_action(message):
# do something with message dictionary
# return another dictionary as response
return {'body': 'Message received!'}
rpc_consumer = RPCBlockingConsumer(
rabbit_url=RABBIT_URL,
exchange=EXCHANGE,
routing_key=ROUTING_KEY,
request_action=my_custom_action,
reconnect_attempts=RECONNECT_ATTEMPTS)
rpc_consumer.start()
```
### RPCBlockingClient
This class connects to RabbitMQ, binds an 'exchange', then allows you to send a `message` to a `routing_key` using the `call` method. This method will block until it receives a response message from the RPC queue. The `call` method returns a dictionary representing the received message.
**Example:**
```python
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
rpc_client = RPCBlockingClient(rabbit_url=RABBIT_URL, exchange=EXCHANGE)
message = {
'body': 'Some message body'
}
# response will be a dictionary of the response message from RPCBlockingConsumer
response = rpc_client.call(routing_key='name_of_routing_key', message=message)
```
### Sender
This class sends messages to a 'direct' exchange, where only one consumer will receive the message.
**Example:**
```python
from pika_pack import Sender
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
sender = Sender(rabbit_url=RABBIT_URL, exchange=EXCHANGE)
message = {
'body': 'Some message body'
}
sender.send('some_routing_key', message)
```
### Broadcaster
This class sends messages to a 'fanout' exchange, where all consumers will receive the message.
**Example:**
```python
from pika_pack import Broadcaster
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
broadcaster = Broadcaster(rabbit_url=RABBIT_URL, exchange=EXCHANGE)
message = {
'body': 'Some message body'
}
broadcaster.send('some_routing_key', message)
```
### Receiver
This class receives messages from a 'direct' exchange, where only one consumer will receive the message.
**Example:**
```python
from pika_pack import Receiver
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
ROUTING_KEY = 'some_routing_key'
RECONNECT_ATTEMPTS = 3
def my_custom_action(message):
# do something with message dictionary this
# function doesn't need to return anything
pass
receiver = Receiver(
rabbit_url=RABBIT_URL,
exchange=EXCHANGE,
routing_key=ROUTING_KEY,
request_action=my_custom_action,
reconnect_attempts=RECONNECT_ATTEMPTS)
receiver.start()
```
### Listener
This class receives messages from a 'fanout' exchange, where all consumers will receive the message.
**Example:**
```python
from pika_pack import Listener
RABBIT_URL = 'rabbit server connection URL'
EXCHANGE = 'name_of_exchange'
ROUTING_KEY = 'some_routing_key'
RECONNECT_ATTEMPTS = 3
def my_custom_action(message):
# do something with message dictionary this
# function doesn't need to return anything
pass
listener = Listener(
rabbit_url=RABBIT_URL,
exchange=EXCHANGE,
routing_key=ROUTING_KEY,
request_action=my_custom_action,
reconnect_attempts=RECONNECT_ATTEMPTS)
listener.start()
```