特に、私は現在、次の関数を使用してクライアントへの接続が有効かどうかを確認しようとしています。
def mongodb_connect(client_uri):
try:
return pymongo.MongoClient(client_uri)
except pymongo.errors.ConnectionFailure:
print "Failed to connect to server {}".format(client_uri)
次に、この関数を次のように使用します。
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
無効なURIが指定された場合、最後の行で例外をキャッチしてスローする方法はありますか?最初はそれがConnectionFailureの目的だと思っていましたが(接続時にこれが検出される可能性があります)、私は間違っていました。
実行に失敗した無効なURIでプログラムを実行した場合、KeyboardInterruptを発行すると次の結果が得られます。
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)
のserverSelectionTimeoutMS
キーワードパラメータはpymongo.mongo_client.MongoClient
、ドライバがサーバーへの接続を試行する時間を制御します。デフォルト値は30秒です。
通常の接続時間と互換性のある非常に低い値に設定して、すぐにエラーを報告してください。その後、接続試行をトリガーするためにDBをクエリする必要があります。
>>> maxSevSelDelay = 1 # Assume 1ms maximum server selection delay
>>> client = pymongo.MongoClient("someInvalidURIOrNonExistantHost",
serverSelectionTimeoutMS=maxSevSelDelay)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> client.server_info()
これが発生しpymongo.errors.ServerSelectionTimeoutError
ます。
¹ どうやら設定serverSelectionTimeoutMS
に0
特定のケースでは、あなたのサーバは非常に低いレイテンシー(例えば非常に軽い負荷で、「ローカル」サーバーの場合)を持っているかもしれないでも仕事を
その例外をキャッチして適切に処理するかどうかは、あなた次第です。そのような何か:
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)
表示されます:
No servers found yet
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加