diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -19,6 +19,7 @@ work. One should use importlib as the pu # Bootstrap-related code ###################################################### +import time CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin' @@ -764,18 +765,28 @@ class _FileFinder: self._path_cache = set() self._relaxed_path_cache = set() self._cache_refresh = 0 + self._last_time = -1.0 def find_module(self, fullname): """Try to find a loader for the specified module.""" tail_module = fullname.rpartition('.')[2] - try: - mtime = _os.stat(self.path).st_mtime - except OSError: - mtime = -1 - if mtime != self._path_mtime or _cache_refresh != self._cache_refresh: + invalidate_cache = False + if _cache_refresh != self._cache_refresh: + invalidate_cache = True + self._cache_refresh = _cache_refresh + else: + now = time.time() + if now >= self._last_time + 1.0: + self._last_time = now + try: + mtime = _os.stat(self.path).st_mtime + except OSError: + mtime = -1 + if mtime != self._path_mtime: + invalidate_cache = True + self._path_mtime = mtime + if invalidate_cache: self._fill_cache() - self._path_mtime = mtime - self._cache_refresh = _cache_refresh # tail_module keeps the original casing, for __file__ and friends if _relax_case(): cache = self._relaxed_path_cache