标签:
1.首先要安装rabbitmq:sudo apt-get install rabbitmq-server。
2.要在python中使用rabbitmq服务,可以使用现成的类库pika、txAMQP或者py-amqplib,这里选择了pika: sudo pip install pika。
举例:
发送消息
#/usr/bin/env python # coding=utf-8 import pika import time def rab_cli(): connection=pika.BlockingConnection(pika.ConnectionParameters(‘127.0.0.1‘)) channel=connection.channel() channel.queue_declare(queue=‘my1‘) channel.basic_publish(exchange=‘‘,routing_key=‘my1‘,body=‘go go go‘) print ‘send"go go go"‘ connection.close() if __name__==‘__main__‘: rab_cli()
接收消息
#/usr/bin/env python #coding=utf-8 import pika connection=pika.BlockingConnection(pika.ConnectionParameters(‘127.0.0.1‘)) channel=connection.channel() channel.queue_declare(‘my1‘) def callback(ch,method,properties,body): print ‘receiced %r‘%(body,) channel.basic_consume(callback,queue=‘my1‘,no_ack=True) print‘receicing...‘ channel.start_consuming()
标签:
原文地址:http://www.cnblogs.com/yeming/p/4808395.html