本文整理汇总了Python中redis.StrictRedis.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.subscribe方法的具体用法?Python StrictRedis.subscribe怎么用?Python StrictRedis.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.subscribe方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Command
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import subscribe [as 别名]
class Command(BaseCommand):
args = ''
help = 'The daemon to run along side the stats viewer.'
def handle(self, *args, **options):
print("Starting the IRC persistence daemon (ctrl+c to stop it)")
self.redis = StrictRedis(host='localhost', port=6379, db=0).pubsub()
self.redis.subscribe('in')
try:
for msg in self.redis.listen():
message = loads(msg['data'])
if message['version'] == 1:
if message['type'] == 'privmsg':
Line.objects.create(time=datetime.now(),
message=message['data']['message'],
nick=Nick.objects.get_or_create(name=message['data']['sender'],
server=Server.objects.get_or_create(name='irc.freenode.net')[0])[0],
channel=Channel.objects.get_or_create(name=message['data']['channel'],
server=Server.objects.get_or_create(name='irc.freenode.net')[0])[0],
action_type=LINE_ACTION_TYPE_MESSAGE,)
except KeyboardInterrupt:
print("Killing the IRC persistence daemon")
示例2: KVDB
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import subscribe [as 别名]
class KVDB(object):
""" A wrapper around the Zato's key-value database.
"""
def __init__(self, conn=None, config=None, decrypt_func=None):
self.conn = conn
self.config = config
self.decrypt_func = decrypt_func
def init(self):
config = {}
if self.config.get('host'):
config['host'] = self.config.host
if self.config.get('port'):
config['port'] = int(self.config.port)
if self.config.get('db'):
config['db'] = int(self.config.db)
if self.config.get('password'):
config['password'] = self.decrypt_func(self.config.password)
if self.config.get('socket_timeout'):
config['socket_timeout'] = float(self.config.socket_timeout)
if self.config.get('connection_pool'):
split = self.config.connection_pool.split('.')
module, class_name = split[:-1], split[-1]
mod = import_module(module)
config['connection_pool'] = getattr(mod, class_name)
if self.config.get('charset'):
config['charset'] = self.config.charset
if self.config.get('errors'):
config['errors'] = self.config.errors
if self.config.get('unix_socket_path'):
config['unix_socket_path'] = self.config.unix_socket_path
self.conn = StrictRedis(**config)
def pubsub(self):
return self.conn.pubsub()
def publish(self, *args, **kwargs):
return self.conn.publish(*args, **kwargs)
def subscribe(self, *args, **kwargs):
return self.conn.subscribe(*args, **kwargs)
def translate(self, system1, key1, value1, system2, key2, default=''):
return self.conn.hget(
_KVDB.SEPARATOR.join(
(_KVDB.TRANSLATION, system1, key1, value1, system2, key2)), 'value2') or default
def copy(self):
""" Returns an KVDB with the configuration copied over from self. Note that
the object returned isn't initialized, in particular, the connection to the
database won't have been initialized.
"""
kvdb = KVDB()
kvdb.config = self.config
kvdb.decrypt_func = self.decrypt_func
return kvdb
def close(self):
self.conn.connection_pool.disconnect()
# ##############################################################################
# OAuth
def add_oauth_nonce(self, username, nonce, max_nonce_log):
""" Adds an OAuth to the set containing last N used ones for a given username.
"""
key = NONCE_STORE.KEY_PATTERN.format('oauth', username)
# This lets us trim the set to top (last) N nonces
score = timegm(gmtime())
self.conn.zadd(key, score, nonce)
self.conn.zremrangebyrank(key, 0, -max_nonce_log)
def has_oauth_nonce(self, username, nonce):
""" Returns a boolean flag indicating if there's an OAuth nonce for a given
username stored in KVDB.
"""
return self.conn.zscore(NONCE_STORE.KEY_PATTERN.format('oauth', username), nonce)
示例3: KVDB
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import subscribe [as 别名]
class KVDB(object):
""" A wrapper around the Zato's key-value database.
"""
def __init__(self, conn=None, config=None, decrypt_func=None):
self.conn = conn
self.config = config
self.decrypt_func = decrypt_func
def init(self):
config = {}
if self.config.get('host'):
config['host'] = self.config.host
if self.config.get('port'):
config['port'] = int(self.config.port)
if self.config.get('db'):
config['db'] = int(self.config.db)
if self.config.get('password'):
config['password'] = self.decrypt_func(self.config.password)
if self.config.get('socket_timeout'):
config['socket_timeout'] = float(self.config.socket_timeout)
if self.config.get('connection_pool'):
split = self.config.connection_pool.split('.')
module, class_name = split[:-1], split[-1]
mod = import_module(module)
config['connection_pool'] = getattr(mod, class_name)
if self.config.get('charset'):
config['charset'] = self.config.charset
if self.config.get('errors'):
config['errors'] = self.config.errors
if self.config.get('unix_socket_path'):
config['unix_socket_path'] = self.config.unix_socket_path
self.conn = StrictRedis(**config)
def pubsub(self):
return self.conn.pubsub()
def publish(self, *args, **kwargs):
return self.conn.publish(*args, **kwargs)
def subscribe(self, *args, **kwargs):
return self.conn.subscribe(*args, **kwargs)
def translate(self, system1, key1, value1, system2, key2, default=''):
return self.conn.hget(_KVDB.SEPARATOR.join((_KVDB.TRANSLATION, system1, key1, value1, system2, key2)), 'value2') or default
def copy(self):
""" Returns an KVDB with the configuration copied over from self. Note that
the object returned isn't initialized, in particular, the connection to the
database won't have been initialized.
"""
kvdb = KVDB()
kvdb.config = self.config
kvdb.decrypt_func = self.decrypt_func
return kvdb
def close(self):
self.conn.connection_pool.disconnect()