Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python rabbitmq consumers poll for acceptance according to their capabilities

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Add to the receiver:

Channel.basic_qos (prefetch_count=1) # # process one at a time and accept new messages after processing

Sender:

Import pikaconnection = pika.BlockingConnection (pika.ConnectionParameters ('localhost')) channel = connection.channel () channel.queue_declare (queue='hello',durable=True) # # queue persistence, also exists after queue restart Does not guarantee the existence of data # channel.queue_delete (queue= "task_queue") for i in range: channel.basic_publish (exchange='', routing_key='hello', body=str (I) Properties=pika.BasicProperties (delivery_mode=2) # # data persistence) # print ("Sent 'hello worldview'") connection.close ()

Receiving end:

#! / usr/bin/env pythonimport pikaimport timeconnection = 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) # returns the obtained data signal to rabbitmq Channel.basic_qos (prefetch_count=1) # # process one at a time, then accept the new message channel.basic_consume (callback, queue='hello', no_ack=False) print ('[*] Waiting for messages. To exit press CTRL+C') channel.start_consuming ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report