Skip to content

hololinked.core.eventloop.operations.PendingOperations

A dictionary of operations that were not yet collected by the caller.

Decoupled from EventLoop currently and only referenced. Benefits and tradeoffs of doing that not clear.

Source code in repo/hololinked/hololinked/core/eventloop/operations.py
class PendingOperations:
    """
    A dictionary of operations that were not yet collected by the caller.

    Decoupled from EventLoop currently and only referenced. Benefits and tradeoffs of doing that not clear.
    """

    def __init__(self, maxsize: int = 1000) -> None:
        self.maxsize = maxsize
        self.futures = dict()  # type: dict[str, OrderedDict[str, Future]]
        self._lock = threading.Lock()
        # `submit()` is callable from any thread, so a caller can hand a reply over on one thread and
        # come back for it on another.

    def add(self, caller_id: str, token: str, future: Future) -> None:
        """
        Remember one operation under the token handed to the caller.

        Parameters
        ----------
        caller_id: str
            an ID of the caller
        token: str
            the token the caller will come back with
        future: concurrent.futures.Future
            the promise of the operation's reply
        """
        with self._lock:
            futures = self.futures.setdefault(caller_id, OrderedDict())
            futures[token] = future
            while len(futures) > self.maxsize:
                futures.popitem(last=False)

    def claim(self, caller_id: str, token: str) -> Future:
        """
        Take the operation's future out of the registry and hand it to the caller.

        Parameters
        ----------
        caller_id: str
            the caller that was handed the token
        token: str
            the token handed to the caller

        Returns
        -------
        concurrent.futures.Future
            the promise of the operation's reply

        Raises
        ------
        KeyError
            if the token is unknown to this caller, or is already claimed, or was evicted
        """
        with self._lock:
            return self.futures[caller_id].pop(token)

    def clear(self) -> None:
        """Cancel and drop every uncollected reply, for every caller."""
        with self._lock:
            for futures in self.futures.values():
                for future in futures.values():
                    future.cancel()
            self.futures.clear()

Attributes

maxsize instance-attribute

maxsize = maxsize

futures instance-attribute

futures = dict()

Functions

__init__

__init__(maxsize: int = 1000) -> None
Source code in repo/hololinked/hololinked/core/eventloop/operations.py
def __init__(self, maxsize: int = 1000) -> None:
    self.maxsize = maxsize
    self.futures = dict()  # type: dict[str, OrderedDict[str, Future]]
    self._lock = threading.Lock()

add

add(caller_id: str, token: str, future: Future) -> None

Remember one operation under the token handed to the caller.

Parameters:

Name Type Description Default

caller_id

str

an ID of the caller

required

token

str

the token the caller will come back with

required

future

Future

the promise of the operation's reply

required
Source code in repo/hololinked/hololinked/core/eventloop/operations.py
def add(self, caller_id: str, token: str, future: Future) -> None:
    """
    Remember one operation under the token handed to the caller.

    Parameters
    ----------
    caller_id: str
        an ID of the caller
    token: str
        the token the caller will come back with
    future: concurrent.futures.Future
        the promise of the operation's reply
    """
    with self._lock:
        futures = self.futures.setdefault(caller_id, OrderedDict())
        futures[token] = future
        while len(futures) > self.maxsize:
            futures.popitem(last=False)

claim

claim(caller_id: str, token: str) -> Future

Take the operation's future out of the registry and hand it to the caller.

Parameters:

Name Type Description Default

caller_id

str

the caller that was handed the token

required

token

str

the token handed to the caller

required

Returns:

Type Description
Future

the promise of the operation's reply

Raises:

Type Description
KeyError

if the token is unknown to this caller, or is already claimed, or was evicted

Source code in repo/hololinked/hololinked/core/eventloop/operations.py
def claim(self, caller_id: str, token: str) -> Future:
    """
    Take the operation's future out of the registry and hand it to the caller.

    Parameters
    ----------
    caller_id: str
        the caller that was handed the token
    token: str
        the token handed to the caller

    Returns
    -------
    concurrent.futures.Future
        the promise of the operation's reply

    Raises
    ------
    KeyError
        if the token is unknown to this caller, or is already claimed, or was evicted
    """
    with self._lock:
        return self.futures[caller_id].pop(token)

clear

clear() -> None

Cancel and drop every uncollected reply, for every caller.

Source code in repo/hololinked/hololinked/core/eventloop/operations.py
def clear(self) -> None:
    """Cancel and drop every uncollected reply, for every caller."""
    with self._lock:
        for futures in self.futures.values():
            for future in futures.values():
                future.cancel()
        self.futures.clear()