当前位置: 首页>>代码示例>>Python>>正文


Python redis.io方法代码示例

本文整理汇总了Python中redis.io方法的典型用法代码示例。如果您正苦于以下问题:Python redis.io方法的具体用法?Python redis.io怎么用?Python redis.io使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在redis的用法示例。


在下文中一共展示了redis.io方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import redis [as 别名]
# 或者: from redis import io [as 别名]
def __init__(self, key='hyperloglog_counter', cycle_time=5,
                 start_time=None, window=None, roll=False, keep_max=None):
        '''
        A unique item counter. Accurate within 1%, max storage of 12k
        http://redis.io/topics/data-types-intro#hyperloglogs
        '''
        ThreadedCounter.__init__(self, key=key, cycle_time=cycle_time,
                                 start_time=start_time, window=window,
                                 roll=roll, keep_max=keep_max) 
开发者ID:istresearch,项目名称:scrapy-cluster,代码行数:11,代码来源:stats_collector.py

示例2: become_leader

# 需要导入模块: import redis [as 别名]
# 或者: from redis import io [as 别名]
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 
开发者ID:zachm,项目名称:tscached,代码行数:39,代码来源:shadow.py

示例3: detect_text

# 需要导入模块: import redis [as 别名]
# 或者: from redis import io [as 别名]
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/ 
开发者ID:GoogleCloudPlatform,项目名称:cloud-vision,代码行数:52,代码来源:textindex.py


注:本文中的redis.io方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。