pythonrabbitmq消费端根据能力轮询接受-成都快上网建站

pythonrabbitmq消费端根据能力轮询接受

给接收端添加:

成都创新互联公司是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序制作,十载建站对生料搅拌车等多个方面,拥有多年的网站推广经验。

channel.basic_qos(prefetch_count=1)  ##一次处理一个,处理完再接受新消息

发送端:

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello',durable=True)  ##队列持久化,队列重启后也存在,不保证数据是否存在
# channel.queue_delete(queue="task_queue")
for i in range(100):
    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body=str(i),
                          properties=pika.BasicProperties(delivery_mode=2) ##数据持久化
                          )
# print("Sent 'hello world!'")
connection.close()

接收端:

#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello',durable=True)
# channel.queue_bind(queue='hello',exchange='',routing_key='hello')
def callback(ch, method, properties, body):
    # print("aaa")
    print(" [x] Received %r" % body)
    time.sleep(1)
    ch.basic_ack(delivery_tag=method.delivery_tag)  # 给rabbitmq返回已拿到数据信号。

channel.basic_qos(prefetch_count=1)  ##一次处理一个,处理完再接受新消息
channel.basic_consume(callback,
                      queue='hello',
                      no_ack=False)

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

新闻标题:pythonrabbitmq消费端根据能力轮询接受
本文URL:http://kswjz.com/article/jesjdi.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流