pathwaycom/pathway · error · ValueError

cache name `{name}` used more than once

Error message

cache name `{name}` used more than once

What it means

ValueError raised by the disk-cache base class when a cache is constructed with a custom name that was already used by another cache instance in the same process. Names are global per process (tracked in a class-level set), so each custom name must be unique.

Source

Thrown at python/pathway/internals/udfs/caches.py:54

    """On disk cache."""

    _cache: diskcache.Cache
    _name: str | None
    _size_limit: int

    _custom_names: ClassVar[set[str]] = set()

    @trace.trace_user_frame
    def __init__(self, name: str | None = None, size_limit=2**30) -> None:
        """
        Args:
            name: name of the cache. When multiple caches have the same name, they share a storage.
            size_limit: a memory limit of the cache in bytes.
        """
        super().__init__()
        if name is not None:
            if name in self._custom_names:
                raise ValueError(f"cache name `{name}` used more than once")
            self._custom_names.add(name)
        self._name = name
        self._cache = None
        self._size_limit = size_limit

    def make_key(self, args: tuple[Any, ...], kwargs: dict[str, Any]) -> str:
        return str(api.ref_scalar(args, tuple(kwargs.items())))

    def wrap_async(self, func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
        @functools.wraps(func)
        async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
            cache = self._get_cache(func)
            key = self.make_key(args, kwargs)
            if cache is None:
                return await func(*args, **kwargs)
            if key not in cache:
                result = await func(*args, **kwargs)
                cache[key] = result

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Give each cache a unique name, or pass name=None to let Pathway derive the name from the function (module_qualname)
  2. Create the cache once and reuse the single instance across UDFs that should share it
  3. Restart the Python process after re-running pipeline construction in notebooks/tests

Example fix

// before
c1 = pw.udfs.DiskCache(name='shared')
c2 = pw.udfs.DiskCache(name='shared')  # ValueError

// after
cache = pw.udfs.DiskCache(name='shared')
# reuse the same instance where sharing is intended
@pw.udf(cache_strategy=cache)
def f(x: int) -> int: ...
Defensive patterns

Strategy: validation

Validate before calling

from pathway.internals.udfs import caches

def unique_cache_name(name: str) -> str:
    if name in caches.DiskCache._custom_names:
        raise ValueError(f'cache name {name!r} already in use')
    return name

Try / catch

try:
    cache = pw.udfs.DiskCache(name='shared')
except ValueError as e:
    if 'used more than once' in str(e):
        cache = pw.udfs.DiskCache()  # fall back to derived name
    else:
        raise

Prevention

When it happens

Trigger: Creating two DiskCache instances with the same name= argument, e.g. pw.udfs.DiskCache(name='my_cache') used in two @pw.udf decorators or two runs of a builder function within one Python process. Note the docstring says same-named caches share storage, but the implementation actually forbids reuse.

Common situations: A cache factory/helper that tags every UDF with a fixed cache name; tests that rebuild pipelines in the same interpreter; notebooks re-running cells without process restart; refactoring that instantiates the cache twice.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/37386dd2ebb2cf9d. Report an issue: GitHub.