from pymongo.errors import AutoReconnect
defretry_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)definsert(self, insert_dict):pass