MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / checkout

Method checkout

pymongo/synchronous/pool.py:1093–1169  ·  view source on GitHub ↗

Get a connection from the pool. Use with a "with" statement. Returns a :class:`Connection` object wrapping a connected :class:`socket.socket`. This method should always be used in a with-statement:: with pool.get_conn() as connection: connection

(
        self, handler: Optional[_MongoClientErrorHandler] = None
    )

Source from the content-addressed store, hash-verified

1091
1092 @contextlib.contextmanager
1093 def checkout(
1094 self, handler: Optional[_MongoClientErrorHandler] = None
1095 ) -> Generator[Connection, None]:
1096 """Get a connection from the pool. Use with a "with" statement.
1097
1098 Returns a :class:`Connection` object wrapping a connected
1099 :class:`socket.socket`.
1100
1101 This method should always be used in a with-statement::
1102
1103 with pool.get_conn() as connection:
1104 connection.send_message(msg)
1105 data = connection.receive_message(op_code, request_id)
1106
1107 Can raise ConnectionFailure or OperationFailure.
1108
1109 :param handler: A _MongoClientErrorHandler.
1110 """
1111 listeners = self.opts._event_listeners
1112 checkout_started_time = time.monotonic()
1113 if self.enabled_for_cmap:
1114 assert listeners is not None
1115 listeners.publish_connection_check_out_started(self.address)
1116 if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
1117 _debug_log(
1118 _CONNECTION_LOGGER,
1119 message=_ConnectionStatusMessage.CHECKOUT_STARTED,
1120 clientId=self._client_id,
1121 serverHost=self.address[0],
1122 serverPort=self.address[1],
1123 )
1124
1125 conn = self._get_conn(checkout_started_time, handler=handler)
1126
1127 duration = time.monotonic() - checkout_started_time
1128 if self.enabled_for_cmap:
1129 assert listeners is not None
1130 listeners.publish_connection_checked_out(self.address, conn.id, duration)
1131 if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
1132 _debug_log(
1133 _CONNECTION_LOGGER,
1134 message=_ConnectionStatusMessage.CHECKOUT_SUCCEEDED,
1135 clientId=self._client_id,
1136 serverHost=self.address[0],
1137 serverPort=self.address[1],
1138 driverConnectionId=conn.id,
1139 durationMS=duration,
1140 )
1141 try:
1142 with self.lock:
1143 self.active_contexts.add(conn.cancel_context)
1144 yield conn
1145 # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
1146 except BaseException:
1147 # Exception in caller. Ensure the connection gets returned.
1148 # Note that when pinned is True, the session owns the
1149 # connection and it is responsible for checking the connection
1150 # back into the pool.

Callers 5

_check_onceMethod · 0.45
_pingMethod · 0.45
_checkoutMethod · 0.45

Calls 7

_get_connMethod · 0.95
checkinMethod · 0.95
_debug_logFunction · 0.90
addMethod · 0.80
handleMethod · 0.45