Python redis 模块,io() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用redis.io()

项目:tscached    作者:zachm    | 项目源码 | 文件源码
def become_leader(config, redis_client):
    """ tscached can be deployed on multiple servers. Only one of them should exert shadow load.
        We use RedLock (http://redis.io/topics/distlock) to achieve this. If we cannot acquire the
        shadow lock, fail fast. If our server (or this program) crashes, the leader key will expire and
        another server will take over eventually.

        RedLock is (debatably) imperfect, but that's okay with us: our worst case is that some work gets
        done twice -  because we are using Redis as a cache and *not* as a datastore. We're using one of
        the standard Python clientlibs: https://github.com/glasslion/redlock

        This implementation assumes a single-master Redis cluster.

        :param config: dict representing the top-level tscached config
        :param redis_client: redis.StrictRedis
        :return: redlock.RedLock or False
    """
    hostname = socket.gethostname()
    leader_expiration = config['shadow'].get('leader_expiration', 3600) * 1000  # ms expected
    deets = [redis_client]  # no need to reinitialize a redis connection.

    try:
        lock = redlock.RedLock(SHADOW_LOCK_KEY, ttl=leader_expiration, connection_details=deets)
        if lock.acquire():
            # mostly for debugging purposes
            redis_client.set(SHADOW_SERVER_KEY, hostname, px=leader_expiration)
            logging.info('Lock acquired; now held by %s' % hostname)
            return lock
        else:
            other_host = redis_client.get(SHADOW_SERVER_KEY)
            logging.info('Could not acquire lock; lock is held by %s' % other_host)
            return False
    except redis.exceptions.RedisError as e:
        logging.error('RedisError in acquire_leader: ' + e.message)
        return False
    except redlock.RedLockError as e:
        logging.error('RedLockError in acquire_leader: ' + e.message)
        return False
项目:cloud-vision    作者:GoogleCloudPlatform    | 项目源码 | 文件源码
def detect_text(self, input_filenames, num_retries=3, max_results=6):
        """Uses the Vision API to detect text in the given file.
        """
        images = {}
        for filename in input_filenames:
            with open(filename, 'rb') as image_file:
                images[filename] = image_file.read()

        batch_request = []
        for filename in images:
            batch_request.append({
                'image': {
                    'content': base64.b64encode(
                            images[filename]).decode('UTF-8')
                },
                'features': [{
                    'type': 'TEXT_DETECTION',
                    'maxResults': max_results,
                }]
            })
        request = self.service.images().annotate(
            body={'requests': batch_request})

        try:
            responses = request.execute(num_retries=num_retries)
            if 'responses' not in responses:
                return {}
            text_response = {}
            for filename, response in zip(images, responses['responses']):
                if 'error' in response:
                    print("API Error for %s: %s" % (
                            filename,
                            response['error']['message']
                            if 'message' in response['error']
                            else ''))
                    continue
                if 'textAnnotations' in response:
                    text_response[filename] = response['textAnnotations']
                else:
                    text_response[filename] = []
            return text_response
        except errors.HttpError as e:
            print("Http Error for %s: %s" % (filename, e))
        except KeyError as e2:
            print("Key error: %s" % e2)
# [END detect_text]


# The inverted index is based in part on this example:
# http://tech.swamps.io/simple-inverted-index-using-nltk/