Python RabbitMQ 权重设置

消费端recv设置
 
注:设置消费端处理完一条消息后再发另一条
 
channel.basic_qos(prefetch_count=1)
 
由于每一条机器的处理速度不同,所以我们这里就会对应,机器的性能来进行调节,使用如下命令。
 
#_*_coding:utf-8_*_
import pika,time

# 实例话创建socket
connection = pika.BlockingConnection(
        pika.ConnectionParameters('localhost'))

# 声明一个管道/在管道内发消息
channel = connection.channel()


# 为什么再次声明queue名字:如果消费者先运行了,没有声明queue就会报错
# 如果想要防止报错发生,就要定义queue。
#
# 管道内,声明一个队列,queue=queue的名字
# durable=True持久话队列
channel.queue_declare(queue='hello10',durable=True)

#回调函数
# ch 管道内存对象地址
# method 消息发给哪个queue
# body = 消息内容
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    #time.sleep(10)
    # 消息处理完后会向生产端发送确认指令
    ch.basic_ack(delivery_tag=method.delivery_tag)

# 设置消费端处理完一条消息后再发另一条
channel.basic_qos(prefetch_count=1)

# 消费消息
# callback 如果收到消息,就调用callback函数来处理消息
# queue 管道内的队列名字
# no_ack = True 这条消息出没处理完都不会给服务端发确认
channel.basic_consume(
                    callback,
                    queue='hello10',)

print(' [*] Waiting for messages. To exit press CTRL+C')

# 启动后一直运行,没有数据会等待..
channel.start_consuming()
原文地址:https://www.cnblogs.com/xiangsikai/p/8304884.html