RabbitMQ发送端接收端生产者消费者的示例分析
admin
2023-07-13 20:05:10
0

rabbit_conn_send_producer.py
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))#rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()#声明一个管道 在管道里面发消息

# 声明queue
channel.queue_declare(queue='hello')

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello',#queue名字
                      body='Hello World!') #body 发送的消息
print(" [x] Sent 'Hello World!'")
connection.close()

rabbit_conn_recive_consumer.py
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost')) #rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()#声明一个管道 在管道里面收消息

# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#channel.queue_declare(queue='hello')#声明queue


def callback(ch, method, properties, body):#处理消息
    print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
    print(" [x] Received %r" % body)


channel.basic_consume(#消费消息
           callback,#如果收到消息,就调用CALLBACK函数来处理消息
           queue='hello',#从哪个队列里收消息
           no_ack=True) #不需要确认消息是否接收

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()#启动 开始收消息 一直收,没有就卡主

rabbit_conn_recive_consumer_no_ack.py
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika,time

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost')) #rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()#声明一个管道 在管道里面收消息

# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#channel.queue_declare(queue='hello')#声明queue


def callback(ch, method, properties, body):#回调函数
    print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
    time.sleep(5)#模拟消息处理时间
    print(" [x] Received %r" % body)


channel.basic_consume(#消费消息
              callback,#如果收到消息,就调用CALLBACK函数来处理消息
           queue='hello',#从哪个队列里收消息
              #no_ack=True)#no acknowledgement 不确认 开启的话表示不确认消息是否接收 接没接收都不会给服务器端发消息 如果客户端没收到消息就忽略了
              #关闭的话就要服务器就要确认消息是否接收,没有确认到接收消息就会一直保留消息,会自动转到另一个客户端,socket一断,rabbitMQ就将消息转给另一个客户端
              #发送端生产者发送一条消息,被消费者接受者收到了,消费者接收端处理完之后自动给生产者发送端发一个确认,说消息处理完了,然后生产者发送端才会把消息从队列里删除,只要没收到确认就不会删除,如果生产者发送端没收到确认,就会把消息转给另一个消费者接收端
          )

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()#启动 开始收消息 一直收,没有就卡主

相关内容

热门资讯

问题居然在实体卡槽上!美版iP... 6月2日消息,日前,又有博主提前把还没发布的iPhone 18 Pro电池参数给曝光了出来,根据爆料...
2026年618手机购机攻略:... 每年618都是手机换新的最佳窗口期,2026年大促叠加数码国补、平台满减、品牌降价三重福利,新机老机...
8.99万打穿底价!史无前例的... 科技的浪漫,不是将人类送入遥不可及的星辰,而是把曾经高不可攀的科幻,变成柴米油盐里的“论斤卖”。 如...
苹果首款折叠屏iPhone U... PChome 6月2日消息,据知名数码博主@i冰宇宙最新曝光的机模谍照,苹果首款折叠屏手机(内部命名...
特朗普:美伊暂停谈判?假新闻! 当地时间6月2日,美国总统特朗普否认伊朗已停止与调解方沟通,称美国与伊朗之间的对话一直在持续进行。特...
白宫记协晚宴改期至7月,特朗普... 当地时间6月2日,美国总统特朗普表示,此前因突发暴力事件而中断的白宫记协晚宴已确定改期至7月24日举...
特朗普,开掉“不听话的情报头子... 苦撑15个月后,美国国家情报总监加巴德即将离开白宫。她对辞职的解释是:丈夫近期确诊罕见骨癌,自己希望...
面粉染头模仿黄仁勋,农村青年走... “先赔偿5000元肖像侵权使用费,再删除所有视频,要么就起诉你。”这是黄仁勋的模仿者杨洋最近频繁收到...
驻马店非遗“大集”开张 把网络... 顶端新闻记者 王丹/文 李思翰 胡楚昊/图红纸剪出“抵制网络谣言”,糖画写下“不信谣不传谣”……5月...
A股三大指数集体收涨 贵金属涨...   A股三大指数集体收涨 贵金属涨幅居前  【A股三大指数集体收涨 贵金属涨幅居前】6月2日,A股三...