本文整理匯總了Python中redis.client.Redis.pubsub方法的典型用法代碼示例。如果您正苦於以下問題:Python Redis.pubsub方法的具體用法?Python Redis.pubsub怎麽用?Python Redis.pubsub使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.client.Redis
的用法示例。
在下文中一共展示了Redis.pubsub方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RedisSubscriber
# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import pubsub [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()
示例2: RedisSocketManager
# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import pubsub [as 別名]
#.........這裏部分代碼省略.........
def init_connection(self, socket, *args, **kwargs):
super(RedisSocketManager, self).init_connection(socket, *args, **kwargs)
if not kwargs.get("nosync", False):
self.redis.sadd(self.connected_key, socket.sessid)
def activate_endpoint(self, sessid, endpoint):
key = self.make_session_key(sessid, "endpoints")
self.redis.sadd(key, endpoint)
self.notify_socket(sessid, "endpoint_activated", endpoint=endpoint)
def deactivate_endpoint(self, sessid, endpoint):
key = self.make_session_key(sessid, "endpoints")
ret = self.redis.srem(key, endpoint) > 0
if (
ret
): # only notify if the endpoint was actually removed by this manager (or we'll get in infinite loop when disconnect triggers deactivate_endpoint)
self.notify_socket(sessid, "endpoint_deactivated", endpoint=endpoint)
return ret
def active_endpoints(self, sessid):
key = self.make_session_key(sessid, "endpoints")
return self.redis.smembers(key)
def notify_socket(self, sessid, event, *args, **kwargs):
msg = json.dumps(dict(uuid=self.uuid, sessid=sessid, event=event, args=args, kwargs=kwargs))
self.redis.publish("socket.events", msg)
def notify_endpoint(self, endpoint, sessid, event, *args, **kwargs):
msg = json.dumps(dict(uuid=self.uuid, endpoint=endpoint, sessid=sessid, event=event, args=args, kwargs=kwargs))
self.redis.publish("endpoint.events", msg)
def _redis_listener(self):
"""Listens to a Redis PubSub for event messages."""
pubsub = self.redis.pubsub()
pubsub.subscribe(self.event_handlers.keys())
for msg in pubsub.listen():
if msg.get("type") == "message":
handler = self.event_handlers.get(msg.get("channel"))
if handler:
handler(msg)
gevent.sleep(0)
def on_socket_event(self, message):
"""Receiving a socket event from the Redis channel.
"""
msg = json.loads(message.get("data"))
sessid = msg.get("sessid")
event = msg.get("event")
args = msg.get("args", [])
kwargs = msg.get("kwargs", {})
uuid = msg.get("uuid")
if uuid != self.uuid:
# global handling for special events from other managers
if event in ("heartbeat_received", "heartbeat_sent", "endpoint_deactivated"):
socket = self.sockets.get(sessid)
if socket:
if event == "heartbeat_received":
socket.heartbeat()
elif socket == "heartbeat_sent":
socket.heartbeat_sent()
elif socket == "endpoint_deactivated":
endpoint = kwargs.get("endpoint")
ns = socket.active_ns.get(endpoint)
if ns: