【问题标题】:PUBSUB CHANNELS returns empty listPUBSUB CHANNELS 返回空列表
【发布时间】:2019-04-23 12:43:49
【问题描述】:

我有一个如下的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 找到我的 Redis 服务器中的频道列表时,使用“PUBSUB CHANNELS”获取一个空列表,如何列出所有频道。 该程序运行良好。

【问题讨论】:

    标签: python-2.7 redis publish-subscribe


    【解决方案1】:

    PUBSUB 频道

    列出当前活动的频道。活动频道是具有一个或多个订阅者的 Pub/Sub 频道(不包括订阅模式的客户)。

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

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

    【讨论】:

    • 我的理解是 PSUBSCRIBE 订阅广播到与给定模式匹配的频道的消息。是否有任何命令可以列出所有模式并查找每个模式的订阅者数量
    猜你喜欢
    • 2016-07-05
    • 2019-08-18
    • 2021-03-11
    • 2015-10-27
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多