pymongo 如何处理异常 AutoReconnect ,防止数据丢失

Retrying Tutorial

https://github.com/rholder/retrying

在使用pymongo的时候 ,自带的线程池 存在 连接失效

raise AutoReconnect("connection closed")

解决方式,加入retry 防止丢失数据

from pymongo.errors import AutoReconnect

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 insert(self, insert_dict):
    pass

你可能感兴趣的:(mongo,python)