本文整理匯總了Python中redis.client.Redis.zrangebyscore方法的典型用法代碼示例。如果您正苦於以下問題:Python Redis.zrangebyscore方法的具體用法?Python Redis.zrangebyscore怎麽用?Python Redis.zrangebyscore使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.client.Redis
的用法示例。
在下文中一共展示了Redis.zrangebyscore方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: RedisSocketManager
# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import zrangebyscore [as 別名]
#.........這裏部分代碼省略.........
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:
# the sync sender should have sent a disconnect message to the client already, so we keep it quiet(= True)
ns.disconnect(True)
# notify the local listeners
super(RedisSocketManager, self).notify_socket(sessid, event, *args, **kwargs)
def on_endpoint_event(self, message):
"""Receiving an endpoint event from the Redis channel.
"""
msg = json.loads(message.get("data"))
endpoint = msg.get("endpoint")
sessid = msg.get("sessid")
event = msg.get("event")
args = msg.get("args", [])
kwargs = msg.get("kwargs", {})
# notify the local listeners
super(RedisSocketManager, self).notify_endpoint(endpoint, event, *args, sender=sessid, **kwargs)
def _orphan_cleaner(self):
"""This will check and cleanup sockets' data that is left orphaned in Redis due to sockets somehow not being
disconnected properly, the most obvious case being a server crash.
"""
timeout = float(self.config["heartbeat_timeout"])
lock_timeout = 5 # Should finish in less than 5 sec (hopefully much, much faster) or lock gets released
interval = float(self.settings.get("orphan_cleaner_interval", timeout))
interval *= 1 + 0.2 * random.random() # between 100% and 120% so not all managers check at the same time
lock_name = "%s:orphan.cleaner" % self.prefix
while True:
with self.redis.lock(lock_name, timeout=lock_timeout): # one check at a time across all workers
old = int(time.time()) - timeout - 1
orphans = self.redis.zrangebyscore(
self.sockets_key, "-inf", str(old)
) # get all timeouted sockets from the sorted set
if orphans:
logger.warning("Cleaning up %s orphaned sockets..." % len(orphans))
with self.redis.pipeline() as pipe:
for sessid in orphans:
self.clean_redis(sessid, pipe)
pipe.execute()
for sessid in orphans:
self.notify_socket(sessid, "dead")
gevent.sleep(interval)