From 7f16b9676166a3fd4996bb8633b0235838424dfd Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Wed, 16 Feb 2022 22:19:18 -0500 Subject: [PATCH] Make the number of threads configurable --- src/fscacher/cache.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/fscacher/cache.py b/src/fscacher/cache.py index 184c908..3b6fe64 100644 --- a/src/fscacher/cache.py +++ b/src/fscacher/cache.py @@ -11,6 +11,8 @@ lgr = logging.getLogger(__name__) +DEFAULT_THREADS = 60 + class PersistentCache(object): """Persistent cache providing @memoize and @memoize_path decorators @@ -21,7 +23,7 @@ class PersistentCache(object): _cache_var_values = (None, "", "clear", "ignore") - def __init__(self, name=None, tokens=None, envvar=None): + def __init__(self, name=None, tokens=None, envvar=None, walk_threads=None): """ Parameters @@ -34,6 +36,8 @@ def __init__(self, name=None, tokens=None, envvar=None): envvar: str, optional Name of the environment variable to query for cache settings; if not set, `FSCACHER_CACHE` is used + walk_threads: int, optional + Number of threads to use when traversing directory hierarchies """ dirs = appdirs.AppDirs("fscacher") self._cache_file = op.join(dirs.user_cache_dir, (name or "cache")) @@ -54,6 +58,7 @@ def __init__(self, name=None, tokens=None, envvar=None): self.clear() self._ignore_cache = cntrl_value == "ignore" self._tokens = tokens + self._walk_threads = walk_threads or DEFAULT_THREADS def clear(self): try: @@ -142,9 +147,8 @@ def fingerprinter(*args, **kwargs): # and we memoize actually that function return fingerprinter - @staticmethod - def _get_dir_fingerprint(dirpath): + def _get_dir_fingerprint(self, dirpath): dprint = DirFingerprint() - for path, fprint in walk(dirpath): + for path, fprint in walk(dirpath, threads=self._walk_threads): dprint.add_file(path, fprint) return dprint