w11_mq.md
[TOC]
#Python3.5-RabbitMQ基本示例
producer.py
```
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='{"name":"alex"}') print(" [x] Sent 'Hello World!'") connection.close()
```
consumer.py
```
import pika import json connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print(" [x] Received %r" % body) body = json.loads(body.decode("utf-8")) print(type(body)) print("body['name']=%s" % body["name"] ) channel.basic_consume(callback, queue='hello', no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
```
[返回顶部](#top)
#Python3.5-RabbitMQ消息分发轮询
```
durable = True #make queue persistent
delivery_mode = 2 #make message persistent
```
refer:http://www.cnblogs.com/alex3714/articles/5248247.html