gpt4 book ai didi

python-3.x - 我的redis客户端只能得到初始 channel 连接的确认,但不能得到消息

转载 作者:可可西里 更新时间:2023-11-01 11:23:27 26 4
gpt4 key购买 nike

我正在 Python-3 中测试 Redis Pub/Sub。我有一个 python 脚本(客户端 A)向 channel 发布消息。我有另一个 python 脚本(客户端 B)来订阅 channel 并检索消息。这两个脚本位于两个不同的 python 文件中。但是B收不到消息,只能得到连接 channel 的确认。

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!')

我终端的输出: channel 连接

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

最佳答案

您是否必须使用 p.get_message() 而不是 p.listen()

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

关于python-3.x - 我的redis客户端只能得到初始 channel 连接的确认,但不能得到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com