Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check if the client for a MongoDB instance is valid?

In particular, I am currently trying to check if a connection to a client is valid using the following function:

def mongodb_connect(client_uri):     try:         return pymongo.MongoClient(client_uri)     except pymongo.errors.ConnectionFailure:          print "Failed to connect to server {}".format(client_uri) 

I then use this function like this:

def bucket_summary(self):     client_uri = "some_client_uri"     client = mongodb_connect(client_uri)     db = client[tenant_id]     ttb = db.timebucket.count() # If I use an invalid URI it hangs here 

Is there a way to catch and throw an exception at the last line if an invalid URI is given? I initially thought that's what the ConnectionFailure was for (so this could be caught when connecting) but I was wrong.

If I run the program with an invalid URI, which fails to run, issuing a KeyboardInterrupt yields:

File "reportjob_status.py", line 58, in <module> tester.summarize_timebuckets() File "reportjob_status.py", line 43, in summarize_timebuckets ttb = db.timebucket.count() #error File "/Library/Python/2.7/site-packages/pymongo/collection.py", line   1023, in count return self._count(cmd) File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 985, in _count with self._socket_for_reads() as (sock_info, slave_ok): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__ return self.gen.next() File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 699, in _socket_for_reads with self._get_socket(read_preference) as sock_info: File  "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__ return self.gen.next() File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 663, in _get_socket server = self._get_topology().select_server(selector) File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 121, in select_server address)) File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 106, in select_servers self._condition.wait(common.MIN_HEARTBEAT_INTERVAL) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 358, in wait _sleep(delay) 
like image 565
Leeren Avatar asked May 29 '15 21:05

Leeren


People also ask

How do I know if Python is running MongoDB?

Call the method server_info() of the client instance to check MongoDB server running Pymongo Python with the exception called ServerSelectionTimeoutError .

How does MongoDB connect to local instance?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.


2 Answers

The serverSelectionTimeoutMS keyword parameter of pymongo.mongo_client.MongoClient controls how long the driver will try to connect to a server. The default value is 30s.

Set it to a very low value compatible with your typical connection time¹ to immediately report an error. You need to query the DB after that to trigger a connection attempt :

>>> maxSevSelDelay = 1 # Assume 1ms maximum server selection delay >>> client = pymongo.MongoClient("someInvalidURIOrNonExistantHost",                                  serverSelectionTimeoutMS=maxSevSelDelay) //                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >>> client.server_info() 

This will raise pymongo.errors.ServerSelectionTimeoutError.

¹ Apparently setting serverSelectionTimeoutMS to 0 might even work in the particular case your server has very low latency (case of a "local" server with very light load for example)


It is up to you to catch that exception and to handle it properly. Something like that:

try:     client = pymongo.MongoClient("someInvalidURIOrNonExistantHost",                                      serverSelectionTimeoutMS=maxSevSelDelay)     client.server_info() # force connection on a request as the                          # connect=True parameter of MongoClient seems                          # to be useless here  except pymongo.errors.ServerSelectionTimeoutError as err:     # do whatever you need     print(err) 

will display:

No servers found yet 
like image 189
Sylvain Leroux Avatar answered Oct 05 '22 08:10

Sylvain Leroux


Hi to find out that the connection is established or not you can do that :

from pymongo import MongoClient from pymongo.errors import ConnectionFailure client = MongoClient() try:    # The ismaster command is cheap and does not require auth.    client.admin.command('ismaster') except ConnectionFailure:    print("Server not available") 
like image 23
Manochehr Rasouli Avatar answered Oct 05 '22 10:10

Manochehr Rasouli