PYTHON-RABBITMQ Message distribution mechanism

Source: Internet
Author: User
Tags rabbitmq

上一篇中的例子是一个生产者对应一个消费者,那能不能一个生产者对应一个消费者呢? 下面来测试一下,顺便观察一下它的分发策略。。。

Step one: Edit the producer Code First (rabbit_send.py)

#top1:导入pika模块import osBASE_DIR = os.path.dirname(os.path.abspath(__file__))import pika#top2:建立socketconnection = pika.BlockingConnection(pika.ConnectionParameters(‘localhost‘))#top3:声明管道channel = connection.channel()#top4:在管道中声明Queue,Queue的名字是‘exclusive‘(随意)channel.queue_declare(queue=‘exclusive‘)#top5:在管道内发送消息channel.basic_publish(exchange=‘‘,                      routing_key=‘exclusive‘,  #queue名称                      body=‘Let s go!‘)             #消息内容#top6:关闭队列connection.close()

Step two: Edit the Consumer code (rabbit_receive.py)

  #top1: Import pika Module Imports Osbase_dir = Os.path.dirname (Os.path.abspath (__file__)) Import Pika#top2: Establish scoketconnection = Pika. Blockingconnection (Pika. Connectionparameters (' localhost ')) #top3: Declare the Channel channel = Connection.channel () #top4: Declare Queuechannel.queue_declare ( Queue= ' exclusive ') #top5: Defines a function that processes messages (referred to as callback functions) def callback (ch, method, properties, body): Print ("[x] Received%r"% body #top6: Receives the message Channel.basic_consume (#消费消息 callback, #如果收到消息, calls the callback function to process the message Qu  Eue= ' exclusive ', no_ack=true) #top7: The start here is running as long as it moves together, because it's not just a channel.start_consuming ()  
定义好生产者和消费者后,执行一个生产者多个消费者进行测试。测试结果是消息的接收机制是轮询的,生产者每发送一次消息,都由消费者轮流来接收。接下来考虑一个情况,现在的代码是消费者接收到消息后调用callback函数去处理消息立刻打印,但是如果我的处理过程需要30秒的时间,恰好在这30秒的时间内消费者宕机了,这个消息还没有处理完,比如我有一个转账的业务,那转到一半宕机了,那咋整?应该有一个确认机制来确定到底是不是处理完了,消费者应该发送一个确认给生产者,然后生产者才把消息从消息队列里删除;还是纠结。。。。那消费者处理到一半宕机了,还怎么给生产者发确认。。。。还用刚才的代码来测试,把在消费者处理消息的函数中加入一个time.sleep(30),再print一句话来模拟处理时间,再执行生产者和多个消费者,假如第一个消费者接收到消息我们把它停止,再观察别的消费者,没反应。。。。什么鬼?消息丢了!!!那我们回过头来把no_ack=True注释掉,这个的意思是"不确认",再测试。结果是把第一个消费者断了,第二个消费者继续处理消息,保证消息被处理完,那为什么生产者知道消费者宕机了呢?因为socket断了,它是连接RabbitMQ的,它断了自然而然就知道消费者宕机了。。一般我们不需要加no_ack=True参数,只有那些对生产者不关心的消息可以加上。

PYTHON-RABBITMQ Message distribution mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.