def _remove_module_cache()

in proxy_worker/utils/dependency.py [0:0]


    def _remove_module_cache(path: str):
        """Remove module cache if the module is imported from specific path.
        This will not impact builtin modules

        Parameters
        ----------
        path: str
            The module cache to be removed if it is imported from this path.
        """
        if not path:
            return

        not_builtin = set(sys.modules.keys()) - set(sys.builtin_module_names)

        # Don't reload proxy_worker
        to_be_cleared_from_cache = set([
            module_name for module_name in not_builtin
            if not module_name.startswith('proxy_worker')
        ])

        for module_name in to_be_cleared_from_cache:
            module = sys.modules.get(module_name)
            if not isinstance(module, ModuleType):
                continue

            # Module path can be actual file path or a pure namespace path.
            # Both of these has the module path placed in __path__ property
            # The property .__path__ can be None or does not exist in module
            try:
                # Safely check for __path__ and __file__ existence
                module_paths = set()
                if hasattr(module, '__path__') and module.__path__:
                    module_paths.update(module.__path__)
                if hasattr(module, '__file__') and module.__file__:
                    module_paths.add(module.__file__)

                if any([p for p in module_paths if p.startswith(path)]):
                    sys.modules.pop(module_name)
            except Exception as e:
                logger.warning(
                    'Attempt to remove module cache for %s but failed with '
                    '%s. Using the original module cache.',
                    module_name, e)