Python 2.7 PUBSUB通道返回空列表
我有一个python程序,如下所示Python 2.7 PUBSUB通道返回空列表,python-2.7,redis,publish-subscribe,Python 2.7,Redis,Publish Subscribe,我有一个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
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在我的Redis服务器中使用“PUBSUB channels”查找频道列表时,如何列出所有频道。
这个程序运行得非常好
公共广播子频道
列出当前活动的频道。活动频道是具有一个或多个订阅者的发布/订阅频道(不包括订阅模式的客户端)
您的代码使用PSUBSCRIBE
命令并订阅模式,而不是频道,因此PUBSUB-CHANNELS
返回空列表
此外,您还可以查看该命令,该命令返回模式的数量
公共广播子频道
列出当前活动的频道。活动频道是具有一个或多个订阅者的发布/订阅频道(不包括订阅模式的客户端)
您的代码使用PSUBSCRIBE
命令并订阅模式,而不是频道,因此PUBSUB-CHANNELS
返回空列表
另外,您还可以查看返回模式数的命令。我的理解是PSU订阅广播到与给定模式匹配的频道的消息。是否有任何命令列出所有模式并查找每个模式的订阅者数量我的理解是PSU订阅订阅广播到与给定模式匹配的频道的消息。是否有命令列出所有模式并查找每个模式的订户数