本文整理汇总了Python中pymongo.errors.AutoReconnect方法的典型用法代码示例。如果您正苦于以下问题:Python errors.AutoReconnect方法的具体用法?Python errors.AutoReconnect怎么用?Python errors.AutoReconnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.errors
的用法示例。
在下文中一共展示了errors.AutoReconnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mongo_retry
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def test_mongo_retry():
retries = [2]
self = MagicMock()
self._arctic_lib.arctic.mongo_host = sentinel.host
self._collection.database.client.nodes = set([('a', 12)])
self._arctic_lib.get_name.return_value = sentinel.lib_name
with patch('arctic.decorators._handle_error', autospec=True) as he:
@mongo_retry
def foo(self):
if retries[0] == 2:
retries[0] -= 1
raise OperationFailure('error')
elif retries[0] == 1:
retries[0] -= 1
raise AutoReconnect('error')
return "success"
foo(self)
assert he.call_count == 2
assert isinstance(he.call_args_list[0][0][1], OperationFailure)
assert he.call_args_list[0][0][2] == 1
assert he.call_args_list[0][1] == {'mnodes': ['a:12'],
'mhost': 'sentinel.host',
'l': sentinel.lib_name}
assert isinstance(he.call_args_list[1][0][1], AutoReconnect)
assert he.call_args_list[1][0][2] == 2
示例2: test_mongo_retry_hook_changes
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def test_mongo_retry_hook_changes():
retries = [2]
self = MagicMock()
hook1 = Mock()
register_log_exception_hook(hook1)
hook2 = Mock()
@mongo_retry
def foo(self):
if retries[0] == 2:
retries[0] -= 1
raise OperationFailure('error')
elif retries[0] == 1:
register_log_exception_hook(hook2)
retries[0] -= 1
raise AutoReconnect('error')
return "success"
foo(self)
assert hook1.call_count == 1
assert hook2.call_count == 1
示例3: test__conn_auth_issue
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def test__conn_auth_issue():
auth_timeout = [0]
a = Arctic("host:12345")
sentinel.creds = Mock()
def flaky_auth(*args, **kwargs):
if not auth_timeout[0]:
auth_timeout[0] = 1
raise AutoReconnect()
with patch('arctic.arctic.authenticate', flaky_auth), \
patch('arctic.arctic.get_auth', return_value=sentinel.creds), \
patch('arctic._cache.Cache.__init__', autospec=True, return_value=None), \
patch('arctic.decorators._handle_error') as he:
a._conn
assert he.call_count == 1
assert auth_timeout[0]
示例4: dbUpdate
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def dbUpdate(self, dbName, collectionName, d, flt, upsert=False):
"""向MongoDB中更新数据,d是具体数据,flt是过滤条件,upsert代表若无是否要插入"""
try:
if self.dbClient:
db = self.dbClient[dbName]
collection = db[collectionName]
collection.replace_one(flt, d, upsert)
else:
self.writeLog(text.DATA_UPDATE_FAILED)
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except AutoReconnect as ex:
self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
time.sleep(1)
except ConnectionFailure:
self.dbClient = None
self.writeError(u'数据库连接断开')
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except Exception as ex:
self.writeError(u'dbUpdate exception:{}'.format(str(ex)))
示例5: _receive_data_on_socket
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def _receive_data_on_socket(sock, length):
msg = b""
while length:
try:
chunk = sock.recv(length)
except (IOError, OSError) as exc:
err = None
if hasattr(exc, 'errno'):
err = exc.errno
elif exc.args:
err = exc.args[0]
if err == errno.EINTR:
continue
raise
if chunk == b"":
raise AutoReconnect("connection closed")
length -= len(chunk)
msg += chunk
return msg
示例6: update
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def update(self, identifier, updates):
updates = copy(updates)
unset = {}
for key, value in [(k, v) for k, v in updates.items()]:
if value is None:
del updates[key]
unset[key] = ''
doc = {}
if updates:
doc['$set'] = updates
if unset:
doc['$unset'] = unset
if not doc:
return
while True:
try:
self.collection.update_one({'id': identifier}, doc)
return
except AutoReconnect: # pragma: no cover
log.exception('update failure, retrying')
time.sleep(1)
示例7: __call__
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def __call__(self, *args, **kwargs):
""" Call the method and handle the AutoReconnect exception gracefully """
start_time = time.time()
for attempt in count():
try:
return self._method(*args, **kwargs)
except AutoReconnect:
duration = time.time() - start_time
if duration >= WAIT_TIME:
break
logger.warning(
'Reconnecting to MongoDB, attempt {} ({:.3f} seconds elapsed)'.
format(attempt, duration))
time.sleep(self.calc_sleep(attempt))
return self._method(*args, **kwargs)
示例8: check_server_up
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def check_server_up(self):
"""Test connection to the server."""
import pymongo
from pymongo.errors import AutoReconnect, ConnectionFailure
# Hostname must exist before continuing
# Some server class (e.g. Docker) will only allocate an IP after the
# container has started.
if not self.hostname:
return False
log.info("Connecting to Mongo at %s:%s" % (self.hostname, self.port))
try:
self.api = pymongo.MongoClient(self.hostname, self.port,
serverselectiontimeoutms=200)
self.api.list_database_names()
# Configure the client with default timeouts in case the server goes slow
self.api = pymongo.MongoClient(self.hostname, self.port)
return True
except (AutoReconnect, ConnectionFailure) as e:
pass
return False
示例9: get_library
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def get_library(self, library):
"""
Return the library instance. Can generally use slicing to return the library:
arctic_store[library]
Parameters
----------
library : `str`
The name of the library. e.g. 'library' or 'user.library'
"""
if library in self._library_cache:
return self._library_cache[library]
try:
error = None
lib = ArcticLibraryBinding(self, library)
lib_type = lib.get_library_type()
except (OperationFailure, AutoReconnect) as e:
error = e
if error:
raise LibraryNotFoundException("Library %s was not correctly initialized in %s.\nReason: %r)" %
(library, self, error))
elif not lib_type:
raise LibraryNotFoundException("Library %s was not correctly initialized in %s." %
(library, self))
elif lib_type not in LIBRARY_TYPES:
raise LibraryNotFoundException("Couldn't load LibraryType '%s' for '%s' (has the class been registered?)" %
(lib_type, library))
instance = LIBRARY_TYPES[lib_type](lib)
self._library_cache[library] = instance
# The library official name may be different from 'library': e.g. 'library' vs 'user.library'
self._library_cache[lib.get_name()] = instance
return self._library_cache[library]
示例10: __send_message
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def __send_message(self, msg):
"""Send a getmore message and handle the response.
"""
client = self.__collection.database.connection
try:
res = client._send_message_with_response(
msg, _connection_to_use=self.__conn_id)
self.__conn_id, (response, dummy0, dummy1) = res
except AutoReconnect:
# Don't try to send kill cursors on another socket
# or to another server. It can cause a _pinValue
# assertion on some server releases if we get here
# due to a socket timeout.
self.__killed = True
raise
try:
response = helpers._unpack_response(response,
self.__id,
*self.__decode_opts)
except CursorNotFound:
self.__killed = True
raise
except AutoReconnect:
# Don't send kill cursors to another server after a "not master"
# error. It's completely pointless.
self.__killed = True
client.disconnect()
raise
self.__id = response["cursor_id"]
assert response["starting_from"] == self.__retrieved, (
"Result batch started from %s, expected %s" % (
response['starting_from'], self.__retrieved))
self.__retrieved += response["number_returned"]
self.__data = deque(response["data"])
示例11: _send_message_with_response
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def _send_message_with_response(self, message, _connection_to_use=None,
_must_use_master=False, **kwargs):
"""Receive a message from Mongo.
Sends the given message and returns a (connection_id, response) pair.
:Parameters:
- `operation`: opcode of the message to send
- `data`: data to send
"""
if _connection_to_use is not None:
if _connection_to_use == -1:
member = self.__master
conn = -1
else:
member = self.__slaves[_connection_to_use]
conn = _connection_to_use
return (conn,
member._send_message_with_response(message, **kwargs)[1])
# _must_use_master is set for commands, which must be sent to the
# master instance. any queries in a request must be sent to the
# master since that is where writes go.
if _must_use_master or self.in_request():
return (-1, self.__master._send_message_with_response(message,
**kwargs)[1])
# Iterate through the slaves randomly until we have success. Raise
# reconnect if they all fail.
for connection_id in helpers.shuffled(range(len(self.__slaves))):
try:
slave = self.__slaves[connection_id]
return (connection_id,
slave._send_message_with_response(message,
**kwargs)[1])
except AutoReconnect:
pass
raise AutoReconnect("failed to connect to slaves")
示例12: dbInsert
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def dbInsert(self, dbName, collectionName, d):
"""向MongoDB中插入数据,d是具体数据"""
try:
# mongodb客户端
if self.dbClient:
db = self.dbClient[dbName]
collection = db[collectionName]
collection.insert_one(d)
else:
self.writeLog(text.DATA_INSERT_FAILED)
# 判断是否连接
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
# 重新连接
self.dbConnect()
except AutoReconnect as ex:
self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
time.sleep(1)
except ConnectionFailure:
self.dbClient = None
self.writeError(u'数据库连接断开')
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except Exception as ex:
self.writeError(u'dbInsert exception:{}'.format(str(ex)))
示例13: dbInsertMany
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def dbInsertMany(self, dbName, collectionName, data_list, ordered=True):
"""
向MongoDB中插入数据,data_list是具体数据 列表
:param dbName:
:param collectionName:
:param data_list:
:param ordered: 是否忽略insert error
:return:
"""
if not isinstance(data_list, list):
self.writeLog(text.DATA_INSERT_FAILED)
return
try:
if self.dbClient:
db = self.dbClient[dbName]
collection = db[collectionName]
collection.insert_many(data_list, ordered=ordered)
else:
self.writeLog(text.DATA_INSERT_FAILED)
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except AutoReconnect as ex:
self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
time.sleep(1)
except ConnectionFailure:
self.dbClient = None
self.writeError(u'数据库连接断开')
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except Exception as ex:
self.writeError(u'dbInsertMany exception:{}'.format(str(ex)))
# ----------------------------------------------------------------------
示例14: dbQuery
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def dbQuery(self, dbName, collectionName, d):
"""从MongoDB中读取数据,d是查询要求,返回的是数据库查询的指针"""
try:
if self.dbClient:
db = self.dbClient[dbName]
collection = db[collectionName]
cursor = collection.find(d)
if cursor:
return list(cursor)
else:
return []
else:
self.writeLog(text.DATA_QUERY_FAILED)
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except AutoReconnect as ex:
self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
time.sleep(1)
except ConnectionFailure:
self.dbClient = None
self.writeError(u'数据库连接断开')
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except Exception as ex:
self.writeError(u'dbQuery exception:{}'.format(str(ex)))
return []
示例15: dbQueryBySort
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import AutoReconnect [as 别名]
def dbQueryBySort(self, dbName, collectionName, d, sortName, sortType, limitNum=0):
"""从MongoDB中读取数据,d是查询要求,sortName是排序的字段,sortType是排序类型
返回的是数据库查询的指针"""
try:
if self.dbClient:
db = self.dbClient[dbName]
collection = db[collectionName]
if limitNum > 0:
cursor = collection.find(d).sort(sortName, sortType).limit(limitNum)
else:
cursor = collection.find(d).sort(sortName, sortType)
if cursor:
return list(cursor)
else:
return []
else:
self.writeLog(text.DATA_QUERY_FAILED)
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except AutoReconnect as ex:
self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
time.sleep(1)
except ConnectionFailure:
self.dbClient = None
self.writeError(u'数据库连接断开')
if self.db_has_connected:
self.writeLog(u'重新尝试连接数据库')
self.dbConnect()
except Exception as ex:
self.writeError(u'dbQueryBySort exception:{}'.format(str(ex)))
return []
# ----------------------------------------------------------------------