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


Python exceptions.WatchError方法代碼示例

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


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

示例1: test_unmoderated

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def test_unmoderated(self):
        # an unmoderated queue is really just testing the number
        # of hits in a given window
        self.queue.redis_conn.zcard = MagicMock(return_value=0)
        self.assertTrue(self.queue.allowed())

        self.queue.redis_conn.zcard = MagicMock(return_value=1)
        self.assertTrue(self.queue.allowed())

        self.queue.redis_conn.zcard = MagicMock(return_value=2)
        self.assertFalse(self.queue.allowed())

        # mock exception raised even with good hits
        self.queue.redis_conn.zcard = MagicMock(return_value=0,
                                                side_effect=WatchError)
        self.assertFalse(self.queue.allowed()) 
開發者ID:istresearch,項目名稱:scrapy-cluster,代碼行數:18,代碼來源:test_redis_throttled_queue.py

示例2: generate

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def generate(cls, next_url):
        with cls.redis_client.pipeline(transaction=True) as pipe:
            while True:
                try:
                    state = cls._generate_state()
                    key = cls.KEY % state
                    pipe.watch(key)
                    if pipe.exists(key):
                        pipe.unwatch(key)
                        continue

                    pipe.multi()
                    pipe.set(key, next_url, ex=CONFIG.AUTH_EXPIRE_TIME)
                    pipe.execute()
                except WatchError:
                    continue
                else:
                    return state 
開發者ID:keakon,項目名稱:Doodle,代碼行數:20,代碼來源:auth.py

示例3: do_extend

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def do_extend(self, additional_time):
        pipe = self.redis.pipeline()
        pipe.watch(self.name)
        lock_value = pipe.get(self.name)
        if lock_value != self.local.token:
            raise LockError("Cannot extend a lock that's no longer owned")
        expiration = pipe.pttl(self.name)
        if expiration is None or expiration < 0:
            # Redis evicted the lock key between the previous get() and now
            # we'll handle this when we call pexpire()
            expiration = 0
        pipe.multi()
        pipe.pexpire(self.name, expiration + int(additional_time * 1000))

        try:
            response = pipe.execute()
        except WatchError:
            # someone else acquired the lock
            raise LockError("Cannot extend a lock that's no longer owned")
        if not response[0]:
            # pexpire returns False if the key doesn't exist
            raise LockError("Cannot extend a lock that's no longer owned")
        return True 
開發者ID:leancloud,項目名稱:satori,代碼行數:25,代碼來源:lock.py

示例4: test_hits

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def test_hits(self):
        '''
        Tests to see if the number of throttle queue hits is within our limit

        @return: True if the queue was below the limit AND atomically updated
        '''
        with self.redis_conn.pipeline() as pipe:
            try:
                pipe.watch(self.window_key)  # ---- LOCK
                value = self.redis_conn.zcard(self.window_key)
                if value < self.limit:
                    # push value into key
                    now = time.time()
                    pipe.multi()
                    pipe.zadd(self.window_key, now, now)
                    # expire it if it hasnt been touched in a while
                    pipe.expire(self.window_key, int(self.window * 2))
                    pipe.execute()

                    return True

            except WatchError:
                # watch was changed, another thread just messed with the
                # queue so we can't tell if our result is ok
                pass

        return False 
開發者ID:istresearch,項目名稱:scrapy-cluster,代碼行數:29,代碼來源:redis_throttled_queue.py

示例5: test_moderated

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def test_moderated(self):
        # a moderated queue should pop ~ every x seconds
        # we already tested the window limit in the unmoderated test
        self.queue.is_moderated = MagicMock(return_value=True)
        self.assertFalse(self.queue.allowed())

        self.queue.is_moderated = MagicMock(return_value=False)
        self.queue.test_hits = MagicMock(return_value=True)
        self.assertTrue(self.queue.allowed())

        # mock exception raised even with good moderation
        self.queue.test_hits = MagicMock(side_effect=WatchError)
        self.assertFalse(self.queue.allowed()) 
開發者ID:istresearch,項目名稱:scrapy-cluster,代碼行數:15,代碼來源:test_redis_throttled_queue.py

示例6: get

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def get(cls, state):
        # 避免重放攻擊,隻允許獲取一次
        with cls.redis_client.pipeline(transaction=True) as pipe:
            try:
                key = cls.KEY % state
                pipe.watch(key)
                next_url = pipe.get(key)
                pipe.multi()
                pipe.delete(key)
                pipe.execute()
            except WatchError:
                return
            else:
                return next_url 
開發者ID:keakon,項目名稱:Doodle,代碼行數:16,代碼來源:auth.py

示例7: _retry

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def _retry(self, partial, try_num=0):
        try:
            return partial()
        except WatchError:  # pragma: no cover
            if try_num < self._num_tries - 1:
                return self._retry(partial, try_num=try_num+1)
            else:
                raise 
開發者ID:brainix,項目名稱:pottery,代碼行數:10,代碼來源:cache.py

示例8: allowed

# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import WatchError [as 別名]
def allowed(self):
        '''
        Check to see if the pop request is allowed

        @return: True means the maximum was not been reached for the current
            time window, thus allowing what ever operation follows
        '''
        # Expire old keys (hits)
        expires = time.time() - self.window
        self.redis_conn.zremrangebyscore(self.window_key, '-inf', expires)

        # check if we are hitting too fast for moderation
        if self.moderation:
            with self.redis_conn.pipeline() as pipe:
                try:
                    pipe.watch(self.moderate_key)  # ---- LOCK
                    # from this point onward if no errors are raised we
                    # successfully incremented the counter

                    curr_time = time.time()
                    if self.is_moderated(curr_time, pipe) and not \
                            self.check_elastic():
                        return False

                    # passed the moderation limit, now check time window
                    # If we have less keys than max, update out moderate key
                    if self.test_hits():
                        # this is a valid transaction, set the new time
                        pipe.multi()
                        pipe.set(name=self.moderate_key,
                                 value=str(curr_time),
                                 ex=int(self.window * 2))
                        pipe.execute()
                        return True

                except WatchError:
                    # watch was changed, another thread just incremented
                    # the value
                    return False

        # If we currently have more keys than max,
        # then limit the action
        else:
            return self.test_hits()

        return False 
開發者ID:istresearch,項目名稱:scrapy-cluster,代碼行數:48,代碼來源:redis_throttled_queue.py


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