Skip to content

proxystore.utils.counter

Atomic counting utilities.

AtomicCounter

AtomicCounter(size: int | None = None)

Thread-safe counter.

Parameters:

  • size (int | None, default: None ) –

    Optional max count upon which an exception will be raised.

Source code in proxystore/utils/counter.py
def __init__(self, size: int | None = None) -> None:
    self._size = size
    self._value = 0
    self._lock = threading.Lock()

increment

increment() -> int

Get current count and increment value.

Returns:

  • int

    Current count.

Raises:

  • ValueError

    If current count is equal to or greater than size.

Source code in proxystore/utils/counter.py
def increment(self) -> int:
    """Get current count and increment value.

    Returns:
        Current count.

    Raises:
        ValueError: If current count is equal to or greater than size.
    """
    with self._lock:
        value = self._value
        if self._size is not None and value >= self._size:
            raise ValueError(f'Max counter size exceeded ({self._size}).')
        self._value += 1
        return value