I am trying to convert my code to send rabbitmq messages via Pika instead. I am having a lot of trouble understanding how to send a simple message using an asynchronous connection (such as SelectConnection).
In my old code, which I use the amqp library I simply declare a class like this:
import amqp as amqp
class MQ():
mqConn = None
channel = None
def __init__(self):
self.connect()
def connect(self):
if self.mqConn is None:
self.mqConn = amqp.Connection(host="localhost", userid="dev", password="dev", virtual_host="/", insist=False)
self.channel = self.mqConn.channel()
elif not self.mqConn.connected:
self.mqConn = amqp.Connection(host="localhost", userid="dev", password="dev", virtual_host="/", insist=False)
self.channel = self.mqConn.channel()
def sendMQ(self, message):
self.connect()
lMessage = amqp.Message(message)
self.channel.basic_publish(lMessage, exchange="DevMatrixE", routing_key="dev_matrix_q")
And then elsewhere in my code I call sendMQ("this is my message"), and then the code continues. I do not need to listen for acknowledgements etc.
Could someone please write a simple class utilizing pika and SelectConnection that would also work to just send a message using sendMQ("this is my message")? I've looked at the pika examples but I don't know how to get around the ioloop and KeyboardInterrupt. I guess I'm just not sure how to make my code continue to run without all these try/excepts... Also, not exactly sure how I can pass my message on through all the callbacks...
Any help is appreciated!
Thanks.
As a first approach I recommend you to start with this pub/sub examples provided at the end of the post. Once you understand this simple example start following the tutorial provided right before the code blocks at the end. The tutorial that has 6 different use cases, with its python examples. With the 5 first steps you will understand the way it works. You should have the clear the concept of exchange (entity that routes the messages to each queue), binding key (key used to connect an exchange and a queue), routing key (key that is sent along with the message from the publisher and that is used by the exchange to route message to one queue or another) and queue (a buffer that can store messages, can have more than 1 (or 1 if wanted) subscriber and that can get messages from more than 1 exchange and based in different binding keys). Besides, there's more than one type of exchange (fanout, topic (this one is probably the one you need)...).
If this all sounds new, please follow the tutorial provided by RabbitMQ:
https://www.rabbitmq.com/tutorials/tutorial-one-python.html
pub.py:
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
sub.py:
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
channel.start_consuming()