def _connect(self, address, lazy_connection=False):
"""Set up a connection to the MongoDB server.
Parameters:
address: MongoDB server address.
lazy_connection: avoid testing if the connection is working while
initializing it.
"""
client = pymongo.MongoClient(address,
serverSelectionTimeoutMS=FLAGS.mongodb_connection_timeout)
if lazy_connection:
return client
# Send a query to the server to see if the connection is working.
try:
client.server_info()
except pymongo.errors.ServerSelectionTimeoutError as e:
logging.error("Unable to connect to %s.", address)
client = None
return client
评论列表
文章目录