资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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消费端根据能力轮询接受
分享路径:http://cdkjz.cn/article/jesjdi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220