PYTHON-RABBITMQ fanout Broadcast Mode

Source: Internet
Author: User
Tags rabbitmq

现实生活中,机器的配置不同,有的性能高,有的性能低,有可能A机器处理了四条消息,B机器一条消息还没处理完,要是按照轮询方式来分发消息的话,那就是A机器一直是空闲,B机器有一大丢消息处理不完。我们在部署集群的时候,往往要添加一个服务器的权重值,那个服务器性能好就多给它点儿任务,那么在RabbitMQ怎么办呢?RabbitMQ里的公平分发是指你有多大本事就干多少活儿,比如A机器处理速度快RabbitMQ就多给它发,B机器处理速度慢就给它少发。RabbitMQ是这样做的,Server端在给Client发消息时会先检查Client端还有多少条消息没有处理完,如果当前队列里消息大于1就不发,队列里为空就发。只需要在**消费者**端加一条短短的代码channel.basic_qos(prefetch_count=1)即可。

We edit two consumers to test receive.py and receive2.py


After editing, open two consumer, with the producer to send the message test, you can see that when the receive2 is not finished processing, the producer's message will not be sent to it.

以上例子都是一对一的发消息,也就是说生产者发一条消息,只有一个消费者能接收,那现在学习一下让所有消费者都能接收到(广播效果),这时候就要用到Exchange了,Exchange一端接收生产者的消息,一段是把消息投递到Queue中,Exchange必须精确的知道投递的精确对象,是投递到某一个Queue呢,还是投递到很多的Queue呢,还是删除?Exchange在定义的时候是有类型的,以决定到底哪些Queue符合条件,可以接收消息。我们可以理解它为"转发器"。Exchange类型:

1. Fanout: All queues that are bound to this exchange can receive messages
2. Direct: The only queue that is determined through Routing_key and Exchange can receive information
3, topic: All matches Routing_key (can be the expression) the Routing_key binds the queue can receive the message
4, headers: Through headers decided to send messages to which queue

fanout就是纯广播,只要绑定到此Exchange的队列就能收到消息

See how it's going to come true.
1. Producer Side

Code in the Channel.exchange_declare inside the parameters are not write variables, such as exchange= "Logs", type= "Fanout", but write will error, do not know what reason ....
2, the consumer side

The producer sends the message, opens several consumer side, can see can receive the message simultaneously (consumer side Execution format: Python publisher.py you good).

Note: In the test, I first open the producer side, according to the common sense I open the consumer side is able to receive the message, but the fact is no news .... The reason is very simple, we imagine the function of the radio, when you turn off the radio when you do not hear the radio, only when you turn on the radio to hear, such as 12-point radio, you can only hear the 12-point broadcast, you can not hear the 11:30 broadcast, said the simple point is real-time. This is what we call a subscription and release of the message.

PYTHON-RABBITMQ fanout Broadcast Mode

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.