本文整理匯總了Python中redis.client.Redis.blpop方法的典型用法代碼示例。如果您正苦於以下問題:Python Redis.blpop方法的具體用法?Python Redis.blpop怎麽用?Python Redis.blpop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.client.Redis
的用法示例。
在下文中一共展示了Redis.blpop方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RedisSubscriber
# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import blpop [as 別名]
class RedisSubscriber(RedisNode):
def __init__(self, config, handler):
RedisNode.__init__(self, config)
self._handler = handler
def connect(self):
conf = parse_uri(self._uri)
self._channel = conf.pop('channel', self._name)
self._method = conf.pop('method', 'pubsub')
self._client = Redis(**conf)
if self._method == 'pubsub':
self._pubsub = self._client.pubsub()
self._pubsub.subscribe(self._channel)
def start(self):
self.connect()
self.loop()
def loop(self):
while True:
self._log.debug('in subscriber loop')
raw_request = self.recv()
self._handler(raw_request)
def recv(self):
"""
Return a message as a string from the receiving queue.
"""
self._log.debug('waiting in recv()')
self._lock.acquire()
if self._method == 'pubsub':
msg = self._pubsub.listen().next()['data']
else:
msg = self._client.blpop(self._channel)[1]
self._log.debug('redisclient: %s' % self._name)
self._log.debug('recv -> %s' % msg)
self._lock.release()
return msg
def close(self):
if self._method == 'pubsub':
self._pubsub.unsubscribe(self._channel)
self._client.connection_pool.disconnect()