Skip to content

Commit 0edd1cd

Browse files
committed
PYTHON-1156 - Note 3.0 MongoClient constructor change
1 parent f2dafb6 commit 0edd1cd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pymongo/mongo_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ def __init__(
125125
126126
client = MongoClient('/tmp/mongodb-27017.sock')
127127
128+
.. note:: Starting with version 3.0 the :class:`MongoClient`
129+
constructor no longer blocks while connecting to the server or
130+
servers, and it no longer raises
131+
:class:`~pymongo.errors.ConnectionFailure` if they are
132+
unavailable, nor :class:`~pymongo.errors.ConfigurationError`
133+
if the user's credentials are wrong. Instead, the constructor
134+
returns immediately and launches the connection process on
135+
background threads. You can check if the server is available
136+
like this::
137+
138+
from pymongo.errors import ConnectionFailure
139+
client = MongoClient()
140+
try:
141+
# The ismaster command is cheap and does not require auth.
142+
client.admin.command('ismaster')
143+
except ConnectionFailure:
144+
print("Server not available")
145+
128146
.. warning:: When using PyMongo in a multiprocessing context, please
129147
read :ref:`multiprocessing` first.
130148

0 commit comments

Comments
 (0)