https://blog.csdn.net/qq_29719097/article/details/80650742

当连接异常时,用try   expect, 使用retry获取异常后重复操作


import pymongo
from pymongo.errors import AutoReconnect
from retrying import retry
collection = pymongo.MongoClient(host='192.168.1.222', port=27017,serverSelectionTimeoutMS=5000, socketTimeoutMS=5000).ali.ocr
def retry_if_auto_reconnect_error(exception):
    """Return True if we should retry (in this case when it's an AutoReconnect), False otherwise"""
    return isinstance(exception, AutoReconnect)
#连接失效异常,自动重连
@retry(retry_on_exception=retry_if_auto_reconnect_error, stop_max_attempt_number=2, wait_fixed=2000)
def insertmongo(collection,mydict):
    collection.insert_one(mydict)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2021-06-02
  • 2021-10-16
猜你喜欢
  • 2022-12-23
  • 2021-09-25
  • 2022-03-08
  • 2021-09-01
  • 2022-12-23
  • 2021-05-24
  • 2021-11-20
相关资源
相似解决方案