HTML5技术

【最全 干货 实例】 缓存手册(Memcached、redis、RabbitMQ) - 索宁(9)

字号+ 作者:H5之家 来源:H5之家 2016-09-01 12:00 我要评论( )

import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(host = ))channel = connection.channel()channel.queue_declare(queue = ) def callback(ch, method, properties, body): % body) im

import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host=)) channel = connection.channel() channel.queue_declare(queue=) def callback(ch, method, properties, body): % body) import time time.sleep(10) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback, queue=, no_ack=False) ) channel.start_consuming()

消费者

2、durable 消息不丢失

需要改两处地方

pika connection = pika.BlockingConnection(pika.ConnectionParameters(host=)) channel = connection.channel() # make message persistent channel.queue_declare(queue=, durable=True) channel.basic_publish(exchange='', routing_key=, body=, properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) ) connection.close()

生产者

#!/usr/bin/env python # -*- coding:utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host=)) channel = connection.channel() # make message persistent channel.queue_declare(queue=, durable=True) def callback(ch, method, properties, body): % body) import time time.sleep(10) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback, queue=, no_ack=False) ) channel.start_consuming()

消费者

3、消息获取顺序

默认情况下,消费者拿消息队列里的数据是按平均分配,例如:消费者1 拿队列中 奇数 序列的任务,消费者2 拿队列中 偶数 序列的任务。

channel.basic_qos(prefetch_count=1) 表示谁来谁取,不再按照奇偶数排列,这个性能较高的机器拿的任务就多

#!/usr/bin/env python # -*- coding:utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters(host=)) channel = connection.channel() # make message persistent channel.queue_declare(queue=) def callback(ch, method, properties, body): % body) import time time.sleep(10) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(callback, queue=, no_ack=False) ) channel.start_consuming()

消费者

4、发布订阅

发布订阅和简单的消息队列区别在于,发布订阅者会将消息发送给所有的订阅者,而消息队列中的数据被消费一次便消失。所以,RabbitMQ 实现发布订阅时,会为每一个订阅者创建一个队列,而发布者发布消息的时候,会将消息放置在所有相关的队列中。

 exchange type = fanout

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 快来一起打飞机、大牛带你用最2代码、写出外挂版微信打飞机 - 索宁

    快来一起打飞机、大牛带你用最2代码、写出外挂版微信打飞机 - 索宁

    2016-08-04 12:00

网友点评
<