python 操作 RabbitMQ示例

👨‍💻个人简介: 深度学习图像领域工作者
🎉总结链接:
             链接中主要是个人工作的总结,每个链接都是一些常用demo,代码直接复制运行即可。包括:
                    📌1.工作中常用深度学习脚本
                    📌2.torch、numpy等常用函数详解
                    📌3.opencv 图片、视频等操作
                    📌4.个人工作中的项目总结(纯干活)
              链接: https://blog.csdn.net/qq_28949847/article/details/128552785
🎉视频讲解: 以上记录,通过B站等平台进行了视频讲解使用,可搜索 ‘Python图像识别’ 进行观看
              B站:Python图像识别
              抖音:Python图像识别
              西瓜视频:Python图像识别


1. 发送消息

import pika

# http://localhost:15672/#/users

# 登录账户、密码
credentials = pika.PlainCredentials('admin', '123456')
# credentials = pika.PlainCredentials('guest', 'guest')
# IP、端口号
parameters = pika.ConnectionParameters('localhost', 5672, '/', credentials)

# 建立与 RabbitMQ 服务器的连接
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
# fanout模式
channel.exchange_declare(exchange='test3', exchange_type='fanout', durable=True)
# topic模式
# channel.exchange_declare(exchange='test', exchange_type='topic', durable=True)


# topic模式,给test3队列发送消息
# channel.basic_publish(exchange='', routing_key='test3', body='55')
# fanout模式,给test3交换价发送消息
channel.basic_publish(exchange='test3', routing_key='', body='55')

print(" [x] Sent 'Hello World!'")

# 关闭连接
connection.close()

2. 接收消息


import pika
import numpy as np
import base64
import cv2

# 登录账户、密码
credentials = pika.PlainCredentials('admin', '123456')
# credentials = pika.PlainCredentials('guest', 'guest')
# IP、端口号
parameters = pika.ConnectionParameters('localhost', 5672, '/', credentials)

# 建立与 RabbitMQ 服务器的连接
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

def callback(ch, method, properties, body):
    print('接收:', body)


# 接收消息并指定回调函数
channel.basic_consume(queue='test3',
                      auto_ack=True,
                      on_message_callback=callback)

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

注意:topic模式 和 fanout模式 在发送消息时,发送代码有区别。
topic模式:给固定的test3队列发送消息,channel.basic_publish(exchange='', routing_key='test3', body='55')
fanout模式,给test3交换机发送消息channel.basic_publish(exchange='test3', routing_key='', body='55')
topic模式同fanout模式详解:https://blog.csdn.net/weixin_45144837/article/details/104335115
安装链接:https://blog.csdn.net/qq_35719977/article/details/127165878

报错:

  1. 实际使用中,出现了一个问题,报错如下:pika.exceptions.StreamLostError: Stream connection lost: ConnectionResetErro
    原因: pika 默认不设置这个heatbeat参数,生产者在一定时间内和服务端没有数据来往,服务端会自动断开连接,不会一直保持connection状态。heatbeat=0不发送心跳,服务端永远不会断开这个连接;,rabbitmq 的日志显示missed heartbeats from client, timeout: 60s
    修改成如下代码即可解决:
credentials = pika.PlainCredentials('admin', '123456')
parameters = pika.ConnectionParameters('192.168.31.22', 5672, '/', credentials, heartbeat=0)
connection = pika.BlockingConnection(parameters)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python图像识别

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值