Archive
How To Install RabbitMQ Server and pika on Ubuntu 10.10
RabbitMQ provides a messaging mechanism for applications, based on a protocol called AMQP. pika is one of the python libraries that understands AMQP.
Here are the steps I took:
Install or upgrade python
$ sudo apt-get install python
Install python-pip and git-core, since the pika installation depends on these packages.
$ sudo apt-get install python-pip git-core
Install the RabbitMQ server package
$ sudo apt-get install rabbitmq-server
Install pika using the pip package management tool
$ sudo pip install pika==0.5.2
Create the code for the sender application, send.py
$ vi send.py
#!/usr/bin/env python
import pika
connection = pika.AsyncoreConnection(pika.ConnectionParameters(host='localhost', credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()
channel.queue_declare(queue='test')
channel.basic_publish(exchange='',routing_key='test',body='Hello World!')
print " [x] Sent 'Hello World!'"
Create the code for the receiver application, receive.py
$ vi receive.py
#!/usr/bin/env python
import pika
connection = pika.AsyncoreConnection(pika.ConnectionParameters(host='127.0.0.1',credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()
channel.queue_declare(queue='test')
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, header, body):
print " [x] Received %.20r" % (body,)
channel.basic_consume(callback,queue='test',no_ack=True)
pika.asyncore_loop()
Check that RabbitMQ is running.
$ sudo rabbitmqctl status
Status of node ‘rabbit@web-server’ …
[{running_applications,[{rabbit,”RabbitMQ”,”1.8.0″},
{mnesia,”MNESIA CXC 138 12″,”4.4.12″},
{os_mon,”CPO CXC 138 46″,”2.2.4″},
{sasl,”SASL CXC 138 11″,”2.1.8″},
{stdlib,”ERTS CXC 138 10″,”1.16.4″},
{kernel,”ERTS CXC 138 10″,”2.13.4″}]},
{nodes,[‘rabbit@web-server’]},
{running_nodes,[‘rabbit@web-server’]}]
…done.
Send a message in one window.
$ python send.py
[x] Sent ‘Hello World!’
List the queues and see how many messages are in them.
$ sudo rabbitmqctl list_queues
Listing queues …
test1
…done.
Receive a message in another window.
$ python receive.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received ‘Hello World!’
List the queues again.
$ Listing queues …
test0
…done.
Kill the receive.py
– Use CTRL+C to interrupt and kill the receiver process
Enjoy!
References:
1. RabbitMQ Python & Pika Tutorial
http://www.rabbitmq.com/tutorials/tutorial-one-python.html
2. AMQP