Python 如何判断mongodb服务器是否已启动?
如何判断mongodb服务器是否已从python启动并运行?我目前使用Python 如何判断mongodb服务器是否已启动?,python,mongodb,Python,Mongodb,如何判断mongodb服务器是否已从python启动并运行?我目前使用 try: con = pymongo.Connection() except Exception as e: ... 或者在pymongo函数中有更好的方法可以使用吗?是的,try/except是检查服务器是否启动的一种很好的(pythonic)方法。但是,最好捕获特定的异常(ConnectionFailure): 是的,try/except是检查服务器是否启动的一种很好的(pythonic)方法。但是,最
try:
con = pymongo.Connection()
except Exception as e:
...
或者在pymongo函数中有更好的方法可以使用吗?是的,try/except是检查服务器是否启动的一种很好的(pythonic)方法。但是,最好捕获特定的异常(ConnectionFailure
):
是的,try/except是检查服务器是否启动的一种很好的(pythonic)方法。但是,最好捕获特定的异常(ConnectionFailure
):
对于pymongo的新版本,来自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")
您可以使用serverSelectionTimeoutMS初始化MongoClient,以避免在代码引发异常之前等待20秒左右:
client = MongoClient(serverSelectionTimeoutMS=500) # wait 0.5 seconds in server selection
对于pymongo的新版本,来自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")
您可以使用serverSelectionTimeoutMS初始化MongoClient,以避免在代码引发异常之前等待20秒左右:
client = MongoClient(serverSelectionTimeoutMS=500) # wait 0.5 seconds in server selection