python-2.7 - PUBSUB CHANNELS 返回空列表

标签 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
        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/

相关文章:

用于 max-sum-max 多个列表的 Pythonic 方法

redis - StackExchange.Redis 客户端在订阅 channel 时是否受到限制?

Go lang Redis PubSub 在不同的 go 路由中用于发布和订阅

python - 存储永久数据的最佳实践?

python - 如何处理来自 Python 中另一个类的小部件命令/函数调用?

Python进程不释放内存

unicode - 如何在redis HMSET中保存非Ascii字符?

python - 将我的 docker 连接到外部 docker

google-app-engine - Go 中的 AppEngine 标准环境 Pub/Sub 上下文

node.js - 分布式发布/订阅,每个消息类型只有一个消费者