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://csruizhi.cn/article/jesjdi.html

其他资讯

Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号
友情链接: 网站建设方案 响应式网站设计方案 成都网站制作 网站制作 成都网站设计公司 企业手机网站建设 网站制作 移动手机网站制作 重庆企业网站建设 企业网站建设 定制网站建设 定制网站设计 网站建设方案 高端网站设计 做网站设计 营销型网站建设 手机网站制作 成都网站建设 阿坝网站设计 成都商城网站建设 响应式网站设计 网站建设公司