2

对于 python 客户端,redis 中是否有一些等效于 NUMSUB 命令?

我查看了文档,除了 publish() 方法本身之外找不到任何东西,它返回该频道上的订阅者数量。不过,事后知道有多少订阅者对我来说并不是很有用。

4

2 回答 2

3

在 redis-py 中似乎还没有针对这些用例的干净包装器,我最终使用普通的 redis 命令来获取特定频道的订阅者

r = redis.StrictRedis(**{
    'host': $WhateverHost,
    'port': 6379,
    })

pubsub = r.pubsub()
pubsub.subscribe('MyChannel:ID')
....
subscriber_count = r.execute_command('PUBSUB', 'NUMSUB', 'MyChannel:ID')
于 2015-08-05T12:57:04.653 回答
1

您可以使用 StrictRedis 连接,然后创建一个 pubsub 对象:

pubsub(self, shard_hint=None) method of redis.client.StrictRedis instance
    Return a Publish/Subscribe object. With this object, you can
    subscribe to channels and listen for messages that get published to
    them.

rc = redis.StrictRedis()
ps = rc.pubsub()
ps.subscribe(channel)
numsub = ps.subscription_count

pubsub 对象具有订阅频道的方法,以及一个名为 subscription_count 的字段,该字段提供频道的订阅者数量。

于 2014-07-31T18:32:38.190 回答