2014-06-18 2 views

답변

2

예/시도는 서버가 작동하는지 확인하는 좋은 (pythonic) 방법입니다. MongoClient 워드 프로세서에서 pymongo의 새로운 버전

try: 
    con = pymongo.Connection() 
except pymongo.errors.ConnectionFailure: 
    ... 
2

, : 그러나, 그것은 특정 excpetion (ConnectionFailure)를 잡기 위해 최선의

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") 

당신은 20 초 동안 대기 방지하기 위해 serverSelectionTimeoutMS와 MongoClient init을하거나 코드 전에 예외가 발생합니다 :

client = MongoClient(serverSelectionTimeoutMS=500) # wait 0.5 seconds in server selection 
관련 문제