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

Class AsyncMongoClient

pymongo/asynchronous/mongo_client.py:177–2625  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

175
176
177class AsyncMongoClient(common.BaseObject, Generic[_DocumentType]):
178 HOST = "localhost"
179 PORT = 27017
180 # Define order to retrieve options from ClientOptions for __repr__.
181 # No host/port; these are retrieved from TopologySettings.
182 _constructor_args = ("document_class", "tz_aware", "connect")
183 _clients: weakref.WeakValueDictionary = weakref.WeakValueDictionary() # type: ignore[type-arg]
184
185 def __init__(
186 self,
187 host: Optional[Union[str, Sequence[str]]] = None,
188 port: Optional[int] = None,
189 document_class: Optional[Type[_DocumentType]] = None,
190 tz_aware: Optional[bool] = None,
191 connect: Optional[bool] = None,
192 type_registry: Optional[TypeRegistry] = None,
193 **kwargs: Any,
194 ) -> None:
195 """Client for a MongoDB instance, a replica set, or a set of mongoses.
196
197 .. warning:: Starting in PyMongo 4.0, ``directConnection`` now has a default value of
198 False instead of None.
199 For more details, see the relevant section of the PyMongo 4.x migration guide:
200 :ref:`pymongo4-migration-direct-connection`.
201
202 The client object is thread-safe and has connection-pooling built in.
203 If an operation fails because of a network error,
204 :class:`~pymongo.errors.ConnectionFailure` is raised and the client
205 reconnects in the background. Application code should handle this
206 exception (recognizing that the operation failed) and then continue to
207 execute.
208
209 Best practice is to call :meth:`AsyncMongoClient.close` when the client is no longer needed,
210 or use the client in a with statement::
211
212 async with AsyncMongoClient(url) as client:
213 # Use client here.
214
215 The `host` parameter can be a full `mongodb URI
216 <https://dochub.mongodb.org/core/connections>`_, in addition to
217 a simple hostname. It can also be a list of hostnames but no more
218 than one URI. Any port specified in the host string(s) will override
219 the `port` parameter. For username and
220 passwords reserved characters like ':', '/', '+' and '@' must be
221 percent encoded following RFC 2396::
222
223 from urllib.parse import quote_plus
224
225 uri = "mongodb://%s:%s@%s" % (
226 quote_plus(user), quote_plus(password), host)
227 client = AsyncMongoClient(uri)
228
229 Unix domain sockets are also supported. The socket path must be percent
230 encoded in the URI::
231
232 uri = "mongodb://%s:%s@%s" % (
233 quote_plus(user), quote_plus(password), quote_plus(socket_path))
234 client = AsyncMongoClient(uri)

Calls

no outgoing calls