本文整理匯總了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 []
# ----------------------------------------------------------------------