本文整理匯總了Python中redis.exceptions.RedisError方法的典型用法代碼示例。如果您正苦於以下問題:Python exceptions.RedisError方法的具體用法?Python exceptions.RedisError怎麽用?Python exceptions.RedisError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.exceptions
的用法示例。
在下文中一共展示了exceptions.RedisError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: word_vec
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def word_vec(self, word): # pylint: disable=E0202
"""
This method is mimicking the word_vec method from the Gensim KeyedVector class. Instead of
looking it up from an in memory dict, it
- requests the value from the redis instance, where the key is a combination between
an optional word vector model key and the word itself
- decompresses it
- and finally unpickles it
:param word: string
:returns: numpy array of dim of the word vector model (for Google: 300, 1)
"""
try:
cache_entry = self._redis.hget(self.key, word)
if not cache_entry:
raise KeyError(f'Key {word} does not exist in cache')
return pickle.loads(cache_entry)
except RedisError as exception:
raise RedisKeyedVectorException(f'The connection to Redis failed while trying to '
f'retrieve a word vector. Redis error message: '
f'{exception}')
except TypeError:
return None
示例2: load_keyed_vectors_into_redis
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def load_keyed_vectors_into_redis(self, model_path, idf_weighting='naive'):
"""
This function loops over all available words in the loaded word2vec keyed vectors model
and loads them into the redis instance.
"""
model = KeyedVectors.load(model_path, mmap='r')
nr_train_tokens = sum(token_vocab.count for token_vocab in model.vocab.values())
self.idf_weighting = idf_weighting
try:
for word in tqdm(list(model.vocab.keys())):
if self.idf_weighting == 'naive':
idf = model.vocab[word].count
elif self.idf_weighting == 'log':
idf = np.log(nr_train_tokens / (model.vocab[word].count + 1)) + 1
else:
raise ValueError(f'idf_weighting "{self.idf_weighting}" not available; use '
f'"naive" or "log"')
idf_normalized_vector = model[word] / idf
self._redis.hset(self.key, word, pickle.dumps(idf_normalized_vector))
except RedisError as exception:
raise RedisKeyedVectorException(f'RedisError while trying to load model {model} '
f'into redis: {exception}')
del model
示例3: exception_handler
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def exception_handler(exc, context):
"""
自定義異常處理
:param exc: 別的地方拋的異常就會傳給exc
:param context: 字典形式。拋出異常的上下文(即拋出異常的出處;即拋出異常的視圖)
:return: Response響應對象
"""
# 調用drf框架原生的異常處理方法,把異常和異常出處交給他處理,如果是序列化器異常就直接處理,處理之後就直接返回
response = drf_exception_handler(exc, context)
#如果響應為空表示不是序列化器異常,補充數據庫異常
if response is None:
view = context['view']
if isinstance(exc, DatabaseError) or isinstance(exc, RedisError):
# 數據庫異常
logger.error('[%s] %s' % (view, exc))
response = Response({'message': '服務器內部錯誤'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)
return response
示例4: __call__
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def __call__(self, fn):
"""Function wrapper."""
@wraps(fn)
def wrapped_f(*args, **kwargs):
c = 0
while c <= self.retry_count:
try:
return fn(*args, **kwargs)
except RedisError:
logging.critical("retrying because of this exception - %s",
c)
logging.exception("exception to retry ")
if c == self.retry_count:
raise
c += 1
return wrapped_f
示例5: app
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def app():
app = create_app()
app.config['PROPAGATE_EXCEPTIONS'] = False
@app.route('/api/minimal-mode')
def minimal_mode():
return unicode(g.auth.is_minimal_mode)
@app.route('/api/mysql')
def mysql_error():
raise SQLAlchemyError()
@app.route('/api/redis')
def redis_error():
raise RedisError()
return app
示例6: _bulk_execute
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def _bulk_execute(self, node, keys, command, args=()):
pipeline = node.client.pipeline(transaction=False)
retry = 0
command_pcs = [command, None]
command_pcs.extend(args)
while retry < self.RETRY_COUNT:
for key in keys:
command_pcs[1] = key
pipeline.execute_command(*command_pcs)
try:
values = pipeline.execute(raise_on_error=False)
return zip(keys, values)
except exceptions.RedisError:
LOG.exception('Error executing pipeline at retry %d', retry)
retry += 1
return False
示例7: _scan
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def _scan(self, table, key=None, topic=None):
(pattern, nodes) = self._query_info(table, topic, key)
keys = set()
for node in nodes:
retry = 0
while retry < self.RETRY_COUNT:
LOG.debug('Getting all keys with pattern %s retry %d',
pattern, retry)
try:
node_keys = self._get_all_keys_from_node(node, pattern)
keys.update(node_keys)
break
except exceptions.RedisError:
LOG.exception('Error getting keys from node %s:%s',
node.ip, node.port)
retry += 1
self._cluster.populate_cluster()
if retry == self.RETRY_COUNT:
raise df_exceptions.DBKeyNotFound('ALL KEYS')
return keys
示例8: lock
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def lock(self, resource, ttl):
retry = 0
val = self.get_unique_id()
# Add 2 milliseconds to the drift to account for Redis expires
# precision, which is 1 millisecond, plus 1 millisecond min
# drift for small TTLs.
drift = int(ttl * self.clock_drift_factor) + 2
redis_errors = list()
while retry < self.retry_count:
n = 0
start_time = int(time.time() * 1000)
del redis_errors[:]
for server in self.servers:
try:
if self.lock_instance(server, resource, val, ttl):
n += 1
except RedisError as e:
redis_errors.append(e)
elapsed_time = int(time.time() * 1000) - start_time
validity = int(ttl - elapsed_time - drift)
if validity > 0 and n >= self.quorum:
if redis_errors:
raise MultipleRedlockException(redis_errors)
return Lock(validity, resource, val)
else:
for server in self.servers:
try:
self.unlock_instance(server, resource, val)
except:
pass
retry += 1
time.sleep(self.retry_delay)
return False
示例9: unlock
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def unlock(self, lock):
redis_errors = []
for server in self.servers:
try:
self.unlock_instance(server, lock.resource, lock.key)
except RedisError as e:
redis_errors.append(e)
if redis_errors:
raise MultipleRedlockException(redis_errors)
示例10: test_ignore_event_on_get_task_registry_error
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def test_ignore_event_on_get_task_registry_error(self, mock_redis_get):
"""Check if an event is ignored when a TaskRegistryError is thrown"""
mock_redis_get.side_effect = RedisError
self.task_scheduler.registry.add('mytask', 'git', 'commit', {})
handler = StartedJobHandler(self.task_scheduler)
result = JobResult(0, 1, 'mytask', 'git', 'commit')
event = JobEvent(JobEventType.STARTED, 0, 'mytask', result)
handled = handler(event)
self.assertEqual(handled, False)
示例11: test_ignore_event_on_update_task_registry_error
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def test_ignore_event_on_update_task_registry_error(self, mock_redis_exists):
"""Check if an event is ignored when a TaskRegistryError is thrown"""
mock_redis_exists.side_effect = [False, True, RedisError]
self.task_scheduler.registry.add('mytask', 'git', 'commit', {})
handler = StartedJobHandler(self.task_scheduler)
result = JobResult(0, 1, 'mytask', 'git', 'commit')
event = JobEvent(JobEventType.STARTED, 0, 'mytask', result)
handled = handler(event)
self.assertEqual(handled, False)
示例12: test_ignore_event_on_task_registry_error
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def test_ignore_event_on_task_registry_error(self, mock_redis_get):
"""Check if an event is ignored when a TaskRegistryError is thrown"""
mock_redis_get.side_effect = RedisError
self.task_scheduler.registry.add('mytask', 'git', 'commit', {})
handler = CompletedJobHandler(self.task_scheduler)
result = JobResult(0, 1, 'mytask', 'git', 'commit')
event = JobEvent(JobEventType.COMPLETED, 0, 'mytask', result)
handled = handler(event)
self.assertEqual(handled, False)
示例13: remove
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def remove(self, task_id):
"""Remove a task from the registry.
To remove it, pass its identifier with `task_id` parameter.
When the identifier is not found, a `NotFoundError` exception
is raised.
:param task_id: identifier of the task to remove
:raises NotFoundError: raised when the given task identifier
is not found in the registry
:raises TaskRegistryError: raised when the given task identifier
is not removed from the registry
"""
self._rwlock.writer_acquire()
try:
task_key = self._task_key(task_id)
found = self.conn.exists(task_key)
if not found:
raise NotFoundError(element=str(task_id))
self.conn.delete(task_key)
logger.debug("Task %s removed from the registry", str(task_id))
except RedisError as e:
msg = "Task {} not removed: {}".format(task_id, e)
logger.error(msg)
raise TaskRegistryError(cause=msg)
finally:
self._rwlock.writer_release()
示例14: update
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def update(self, task_id, task):
"""Update a task in the registry.
Update a task stored in the registry using its task identifier. When
the task does not exist, a `NotFoundError` exception will be
raised.
:param task_id: task identifier
:param task: task object
:returns: a task object
:raises TaskRegistryError: raised when the task is not
updated
"""
self._rwlock.writer_acquire()
try:
task_key = self._task_key(task_id)
found = self.conn.exists(task_key)
if not found:
logger.warning("Task %s not found, adding it", str(task_id))
self.conn.set(task_key, pickle.dumps(task))
logger.debug("Task %s updated", str(task_id))
except RedisError as e:
msg = "Task {} not updated: {}".format(task_id, e)
logger.error(msg)
raise TaskRegistryError(cause=msg)
finally:
self._rwlock.writer_release()
示例15: tasks
# 需要導入模塊: from redis import exceptions [as 別名]
# 或者: from redis.exceptions import RedisError [as 別名]
def tasks(self):
"""Get the list of tasks
Retrieve the list of tasks stored in the registry
:returns: a list of tasks
:raises TaskRegistryError: raised when the tasks cannot
be listed
"""
self._rwlock.reader_acquire()
try:
tasks = []
keys = []
match_prefix = "{}*".format(TASK_PREFIX)
total, found = self.conn.scan(match=match_prefix)
keys.extend([f.decode("utf-8") for f in found])
while total != 0:
total, found = self.conn.scan(cursor=total, match=match_prefix)
keys.extend([f.decode("utf-8") for f in found])
keys.sort()
for k in keys:
task_dump = self.conn.get(k)
tasks.append(pickle.loads(task_dump))
return tasks
except RedisError as e:
msg = "Tasks not listed: {}".format(e)
logger.error(msg)
raise TaskRegistryError(cause=msg)
finally:
self._rwlock.reader_release()