當前位置: 首頁>>代碼示例>>Python>>正文


Python Redis.lock方法代碼示例

本文整理匯總了Python中redis.client.Redis.lock方法的典型用法代碼示例。如果您正苦於以下問題:Python Redis.lock方法的具體用法?Python Redis.lock怎麽用?Python Redis.lock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在redis.client.Redis的用法示例。


在下文中一共展示了Redis.lock方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: RedisSocketManager

# 需要導入模塊: from redis.client import Redis [as 別名]
# 或者: from redis.client.Redis import lock [as 別名]
class RedisSocketManager(BaseSocketManager):
    def __init__(self, *args, **kwargs):
        super(RedisSocketManager, self).__init__(*args, **kwargs)

        self.settings = self.config.get("socket_manager", {})

        self.prefix = self.settings.get("key_prefix", "socketio")
        self.buckets_count = self.settings.get("buckets_count", 1000)

        redis_settings = {}
        for k, v in self.settings.items():
            if k.startswith("redis_"):
                k = k.replace("redis_", "", 1)
                if k == "port":
                    v = int(v)
                redis_settings[k] = v

        self.redis_settings = redis_settings

        self.connected_key = "%s:connected" % self.prefix
        self.sockets_key = "%s:sockets" % self.prefix

        lock_factory = lambda sessid: GroupLock(self.redis, self.make_session_key(sessid, "lock"))
        self.locks = DefaultDict(lock_factory)
        self.uuid = str(uuid.uuid1())

        self.event_handlers = {"socket.events": self.on_socket_event, "endpoint.events": self.on_endpoint_event}

        self.jobs = []

    def spawn(self, fn, *args, **kwargs):
        new = gevent.spawn(fn, *args, **kwargs)
        self.jobs.append(new)
        return new

    def start(self):
        self.redis = Redis(**self.redis_settings)

        self.spawn(self._redis_listener)
        self.spawn(self._orphan_cleaner)

    def stop(self):
        gevent.killall(self.jobs)
        self.redis = None

    def clean_redis(self, sessid, client=None):
        single = client is None
        if client is None:
            client = self.redis.pipeline()
        client.delete(self.make_session_key(sessid, "session"))
        client.delete(self.make_session_key(sessid, "lock"))
        client.delete(self.make_session_key(sessid, "endpoints"))
        for qname in virtsocket.QUEUE_NAMES:
            client.delete(self.make_session_key(sessid, "queue:%s" % qname))

        client.zrem(self.sockets_key, sessid)
        client.hdel(self.make_bucket_name("hits", sessid), sessid)

        client.srem(self.connected_key, sessid)
        if single:
            client.execute()

    def detach(self, sessid):
        super(RedisSocketManager, self).detach(sessid)
        self.clean_redis(sessid)
        try:
            del self.locks[sessid]
        except KeyError:
            pass

    def make_bucket_name(self, key, sessid):
        return "%s:%s:b%s" % (self.prefix, key, self.bucket_id(sessid))

    def make_session_key(self, sessid, suffix):
        return "%s:%s:%s" % (self.prefix, sessid, suffix)

    def make_queue(self, sessid, name):
        """Returns a Redis based message queue.
        """
        return RedisQueue(self.redis, self.make_session_key(sessid, "queue:%s" % name))

    def bucket_id(self, sessid):
        """Returns the id of the corresponding bucket for a socket
        
        We'll keep socket hashed data in buckets.
        As we use random sessid these should be normally be quite sparse.
        """
        nid = int(sessid.lstrip("0") or 0)
        return str(nid % self.buckets_count)

    def read_queue(self, queue, **kwargs):
        """Optimized for faster bulk read from Redis, while still supporting ``block`` and ``timeout`` for the first read.
        
        Returns a list of all messages currently in the Queue.
        Raises ``gevent.queue.Empty`` if the queue is empty
        """
        ret = []
        block = kwargs.get("block", True)
        if block:
            ret.append(queue.get(**kwargs))  # block while reading the first
#.........這裏部分代碼省略.........
開發者ID:ryesoft,項目名稱:gevent-socketio,代碼行數:103,代碼來源:socket_manager.py


注:本文中的redis.client.Redis.lock方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。