python-3.x - 我的redis客户端只能得到初始通道连接的确认,但不能得到消息
问题描述
我正在 Python-3 中测试 Redis Pub/Sub。我有一个 python 脚本(客户端 A)向频道发布消息。我有另一个 python 脚本(客户端 B)来订阅频道并检索消息。这两个脚本位于两个不同的 python 文件中。但是,B 不能得到消息,只能得到连接通道的确认。
a = redis.StrictRedis(host='localhost', port=6379, db=0)
a.publish('flag','good job')
#
b = redis.StrictRedis(host='localhost', port=6379, db=0)
p = b.pubsub()
p. subscribe(['flag'])
while True:
for m in p.listen():
if m['type'] == 'subscribe':
print('channel connected')
elif m['type'] == 'message':
print (m['data'])
else:
print('something wrong!')
我的终端的输出:通道已连接
我期待的是:“通道已连接”之后的完整消息内容。
解决方案
可能是你必须使用p.get_message()
而不是p.listen()
?
阅读此https://medium.com/@johngrant/python-redis-pub-sub-6e26b483b3f7
推荐阅读
- python - 在python中将列表分成4等份
- javascript - 如何将多个对象数组转换为单个对象
- jquery - 如何在模态窗口中显示 .pdf 文件?
- meteor - 如何清洁刀片上的物体
- javascript - Angular 5 和 OpenLayers 4:无法读取属性“getEventPixel”和“forEachFeatureAtPixel”
- c++ - 通过初始化列表中的函数初始化变量
- microsoft-graph-api - 在 Microsoft Teams 中创建多个频道只会创建“一些”频道
- python - Pytest“run-around-tests”夹具在一个类中的所有测试之前只运行一次
- vba - VBA - 单击多个合并单元格的是/否时更改单元格值
- c# - WPF 应用程序中的 AddOpenIdConnect