diff --git a/src/sonic-py-common/sonic_py_common/multi_asic.py b/src/sonic-py-common/sonic_py_common/multi_asic.py index a43275fd1ba9..662c01800e35 100644 --- a/src/sonic-py-common/sonic_py_common/multi_asic.py +++ b/src/sonic-py-common/sonic_py_common/multi_asic.py @@ -27,6 +27,10 @@ PORT_ROLE = 'role' +# Dictionary to cache config_db connection handle per namespace +# to prevent duplicate connections from being opened +config_db_handle = {} + def connect_config_db_for_ns(namespace=DEFAULT_NAMESPACE): """ The function connects to the config DB for a given namespace and @@ -237,7 +241,9 @@ def get_all_namespaces(): if is_multi_asic(): for asic in range(num_asics): namespace = "{}{}".format(ASIC_NAME_PREFIX, asic) - config_db = connect_config_db_for_ns(namespace) + if namespace not in config_db_handle: + config_db_handle[namespace] = connect_config_db_for_ns(namespace) + config_db = config_db_handle[namespace] metadata = config_db.get_table('DEVICE_METADATA') if metadata['localhost']['sub_role'] == FRONTEND_ASIC_SUB_ROLE: