添加
deb http://www.rabbitmq.com/debian/ testing main
#wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
#sudo apt-key add rabbitmq-signing-key-public.asc
#sudo apt-get update
#sudo apt-get install rabbitmq-server
#sudo service rabbitmq-server start
#ulimit -n 1024
#sudo pip pika #如果沒有安裝pip的同學,請百度一下安裝方法
#vi send.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()
#vi receive.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()
測試結果
$ python send.py[x] Sent 'Hello World!'
$ python receive.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'