首页 > 解决方案 > 我的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!')

我的终端的输出:通道已连接

我期待的是:“通道已连接”之后的完整消息内容。

标签: python-3.xredis

解决方案


可能是你必须使用p.get_message()而不是p.listen()

阅读此https://medium.com/@johngrant/python-redis-pub-sub-6e26b483b3f7


推荐阅读