def connect(self):
"""Connect to Mongo and return a new connected MotorSocket. Note that
the pool does not keep a reference to the socket -- you must call
maybe_return_socket() when you're done with it.
"""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
if self.max_size and self.motor_sock_counter >= self.max_size:
if self.max_waiters and len(self.queue) >= self.max_waiters:
raise self._create_wait_queue_timeout()
waiter = stack_context.wrap(child_gr.switch)
self.queue.append(waiter)
if self.wait_queue_timeout is not None:
deadline = self.io_loop.time() + self.wait_queue_timeout
timeout = self.io_loop.add_timeout(
deadline,
functools.partial(
child_gr.throw,
pymongo.errors.ConnectionFailure,
self._create_wait_queue_timeout()))
self.waiter_timeouts[waiter] = timeout
# Yield until maybe_return_socket passes spare socket in.
return main.switch()
else:
motor_sock = self.create_connection()
motor_sock.settimeout(self.net_timeout)
return SocketInfo(motor_sock, self.pool_id, self.pair[0])
python类errors()的实例源码
def _check(self, sock_info):
"""This side-effecty function checks if this pool has been reset since
the last time this socket was used, or if the socket has been closed by
some external network error, and if so, attempts to create a new socket.
If this connection attempt fails we reset the pool and reraise the
error.
Checking sockets lets us avoid seeing *some*
:class:`~pymongo.errors.AutoReconnect` exceptions on server
hiccups, etc. We only do this if it's been > 1 second since
the last socket checkout, to keep performance reasonable - we
can't avoid AutoReconnects completely anyway.
"""
error = False
if sock_info.closed:
error = True
elif self.pool_id != sock_info.pool_id:
sock_info.close()
error = True
elif time.time() - sock_info.last_checkout > 1:
if _closed(sock_info.sock):
sock_info.close()
error = True
if not error:
return sock_info
else:
try:
return self.connect()
except socket.error:
self.reset()
raise
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorClient` now opens itself on demand, calling ``open``
explicitly is now optional.
"""
yield self._ensure_connected()
raise gen.Return(self)
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorReplicaSetClient` now opens itself on demand, calling
``open`` explicitly is now optional.
"""
yield self._ensure_connected(True)
primary = self._get_member()
if not primary:
raise pymongo.errors.AutoReconnect('no primary is available')
raise gen.Return(self)
def __getattr__(self, item):
if not self.delegate._file:
raise pymongo.errors.InvalidOperation(
"You must call MotorGridOut.open() before accessing "
"the %s property" % item)
return getattr(self.delegate, item)
def connect(self):
"""Connect to Mongo and return a new connected MotorSocket. Note that
the pool does not keep a reference to the socket -- you must call
maybe_return_socket() when you're done with it.
"""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
if self.max_size and self.motor_sock_counter >= self.max_size:
if self.max_waiters and len(self.queue) >= self.max_waiters:
raise self._create_wait_queue_timeout()
waiter = stack_context.wrap(child_gr.switch)
self.queue.append(waiter)
if self.wait_queue_timeout is not None:
deadline = self.io_loop.time() + self.wait_queue_timeout
timeout = self.io_loop.add_timeout(
deadline,
functools.partial(
child_gr.throw,
pymongo.errors.ConnectionFailure,
self._create_wait_queue_timeout()))
self.waiter_timeouts[waiter] = timeout
# Yield until maybe_return_socket passes spare socket in.
return main.switch()
else:
motor_sock = self.create_connection()
motor_sock.settimeout(self.net_timeout)
return SocketInfo(motor_sock, self.pool_id, self.pair[0])
def _check(self, sock_info):
"""This side-effecty function checks if this pool has been reset since
the last time this socket was used, or if the socket has been closed by
some external network error, and if so, attempts to create a new socket.
If this connection attempt fails we reset the pool and reraise the
error.
Checking sockets lets us avoid seeing *some*
:class:`~pymongo.errors.AutoReconnect` exceptions on server
hiccups, etc. We only do this if it's been > 1 second since
the last socket checkout, to keep performance reasonable - we
can't avoid AutoReconnects completely anyway.
"""
error = False
if sock_info.closed:
error = True
elif self.pool_id != sock_info.pool_id:
sock_info.close()
error = True
elif time.time() - sock_info.last_checkout > 1:
if _closed(sock_info.sock):
sock_info.close()
error = True
if not error:
return sock_info
else:
try:
return self.connect()
except socket.error:
self.reset()
raise
def _create_wait_queue_timeout(self):
return pymongo.errors.ConnectionFailure(
'Timed out waiting for socket from pool with max_size %r and'
' wait_queue_timeout %r' % (
self.max_size, self.wait_queue_timeout))
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorReplicaSetClient` now opens itself on demand, calling
``open`` explicitly is now optional.
"""
yield self._ensure_connected(True)
primary = self._get_member()
if not primary:
raise pymongo.errors.AutoReconnect('no primary is available')
raise gen.Return(self)
def _get_more(self, callback):
"""
Get a batch of data asynchronously, either performing an initial query
or getting more data from an existing cursor.
:Parameters:
- `callback`: function taking parameters (batch_size, error)
"""
if not self.alive:
raise pymongo.errors.InvalidOperation(
"Can't call get_more() on a MotorCursor that has been"
" exhausted or killed.")
self.started = True
self._refresh(callback=callback)
def __getattr__(self, item):
if not self.delegate._file:
raise pymongo.errors.InvalidOperation(
"You must call MotorGridOut.open() before accessing "
"the %s property" % item)
return getattr(self.delegate, item)
def check_deprecated_kwargs(kwargs):
if 'safe' in kwargs:
raise pymongo.errors.ConfigurationError(
"Motor does not support 'safe', use 'w'")
if 'slave_okay' in kwargs or 'slaveok' in kwargs:
raise pymongo.errors.ConfigurationError(
"Motor does not support 'slave_okay', use read_preference")
if 'auto_start_request' in kwargs:
raise pymongo.errors.ConfigurationError(
"Motor does not support requests")
def _check(self, sock_info):
"""This side-effecty function checks if this pool has been reset since
the last time this socket was used, or if the socket has been closed by
some external network error, and if so, attempts to create a new socket.
If this connection attempt fails we reset the pool and reraise the
error.
Checking sockets lets us avoid seeing *some*
:class:`~pymongo.errors.AutoReconnect` exceptions on server
hiccups, etc. We only do this if it's been > 1 second since
the last socket checkout, to keep performance reasonable - we
can't avoid AutoReconnects completely anyway.
"""
error = False
if sock_info.closed:
error = True
elif self.pool_id != sock_info.pool_id:
sock_info.close()
error = True
elif time.time() - sock_info.last_checkout > 1:
if _closed(sock_info.sock):
sock_info.close()
error = True
if not error:
return sock_info
else:
try:
return self.connect()
except socket.error:
self.reset()
raise
def _create_wait_queue_timeout(self):
return pymongo.errors.ConnectionFailure(
'Timed out waiting for socket from pool with max_size %r and'
' wait_queue_timeout %r' % (
self.max_size, self.wait_queue_timeout))
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorClient` now opens itself on demand, calling ``open``
explicitly is now optional.
"""
yield self._ensure_connected()
raise gen.Return(self)
def open(self):
"""Connect to the server.
Takes an optional callback, or returns a Future that resolves to
``self`` when opened. This is convenient for checking at program
startup time whether you can connect.
.. doctest::
>>> client = MotorClient()
>>> # run_sync() returns the open client.
>>> IOLoop.current().run_sync(client.open)
MotorClient(MongoClient('localhost', 27017))
``open`` raises a :exc:`~pymongo.errors.ConnectionFailure` if it
cannot connect, but note that auth failures aren't revealed until
you attempt an operation on the open client.
:Parameters:
- `callback`: Optional function taking parameters (self, error)
.. versionchanged:: 0.2
:class:`MotorReplicaSetClient` now opens itself on demand, calling
``open`` explicitly is now optional.
"""
yield self._ensure_connected(True)
primary = self._get_member()
if not primary:
raise pymongo.errors.AutoReconnect('no primary is available')
raise gen.Return(self)
def _get_more(self, callback):
"""
Get a batch of data asynchronously, either performing an initial query
or getting more data from an existing cursor.
:Parameters:
- `callback`: function taking parameters (batch_size, error)
"""
if not self.alive:
raise pymongo.errors.InvalidOperation(
"Can't call get_more() on a MotorCursor that has been"
" exhausted or killed.")
self.started = True
self._refresh(callback=callback)
def __getattr__(self, item):
if not self.delegate._file:
raise pymongo.errors.InvalidOperation(
"You must call MotorGridOut.open() before accessing "
"the %s property" % item)
return getattr(self.delegate, item)
def _safe_mongo_call(max_retries, retry_interval):
return tenacity.retry(
retry=tenacity.retry_if_exception_type(
pymongo.errors.AutoReconnect),
wait=tenacity.wait_fixed(retry_interval),
stop=(tenacity.stop_after_attempt(max_retries) if max_retries >= 0
else tenacity.stop_never)
)
def create_index(self, keys, name=None, *args, **kwargs):
try:
self.conn.create_index(keys, name=name, *args, **kwargs)
except pymongo.errors.OperationFailure as e:
if e.code is ERROR_INDEX_WITH_DIFFERENT_SPEC_ALREADY_EXISTS:
LOG.info("Index %s will be recreate." % name)
self._recreate_index(keys, name, *args, **kwargs)