本文整理匯總了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)
示例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
示例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/