gpt4 book ai didi

python-2.7 - PUBSUB CHANNELS 返回空列表

转载 作者:可可西里 更新时间:2023-11-01 11:14:31 24 4
gpt4 key购买 nike

我有一个python程序如下

import json
import threading

import redis

CHANNELS_PREFIX = 'client'


class Listener(threading.Thread):
STOP = 1
CONTINUE = 0

def __init__(self, r):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.psubscribe(["%s:*" % CHANNELS_PREFIX])

def reload(self, data):
print "Reloaing", data
return Listener.CONTINUE

def shutdown(self, data):
self.pubsub.unsubscribe()
print "unsubscribed and finished"
return Listener.STOP

def run(self):
for item in self.pubsub.listen():
print item
type = item['type']
if type == 'psubscribe':
continue
data = item['data'].strip()
channel, method_name = item['channel'].split(':')
method = getattr(self, method_name)
if method is not None:
if method(data) == Listener.STOP:
break


class Publisher():

def __init__(self, r):
self.redis = r

def key(self, command):
return "%s:%s" % (CHANNELS_PREFIX, command)

def send(self, command, data):
self.redis.publish(self.key(command), json.dumps(data))

if __name__ == "__main__":
client = Listener(redis.Redis())
client.start()

publisher = Publisher(redis.Redis())

当我执行此命令并尝试使用 redis-cli 使用“PUBSUB CHANNELS”查找我的 Redis 服务器中的 channel 列表时,得到一个空列表,如何列出所有 channel 。该程序运行良好。

最佳答案

PUBSUB CHANNELS

Lists the currently active channels. An active channel is a Pub/Sub channel with one or more subscribers (not including clients subscribed to patterns).

您的代码使用 PSUBSCRIBE 命令并订阅一个模式,而不是一个 channel ,因此 PUBSUB CHANNELS 返回一个空列表。

此外,您可以查看 PUBSUB NUMPAT命令,它返回模式的数量。

关于python-2.7 - PUBSUB CHANNELS 返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55811522/

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