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