sppas 5.1.0

https://sppas.org/

Module sppas.core.config

Class sppasHeartbeat

Description

The last sign of life of something which announces itself.

Whatever announces itself periodically -- a page displaying the traces, the wx interface, any other interlocutor -- is known to be there by the freshness of its last sign of life, and by nothing else. A crash, a kill or a closed tab stops the signs: the silence is the answer. Nothing has to announce a death nobody is left alive to announce.

Example
 >>> beat = sppasHeartbeat(max_age=90.)
 >>> beat.ping()
 >>> beat.alive()
 > True

Constructor

__init__

Create a sppasHeartbeat instance.

Parameters
  • max_age: (float) Age of the last sign of life, in seconds, above which the interlocutor is considered gone.
View Source
def __init__(self, max_age: float=40.0):
    """Create a sppasHeartbeat instance.

    :param max_age: (float) Age of the last sign of life, in seconds,
    above which the interlocutor is considered gone.

    """
    self.__max_age = float(max_age)
    self.__seen = None
    self.__lock = threading.Lock()

Public functions

get_max_age

Return the age above which the interlocutor is considered gone.

View Source
def get_max_age(self) -> float:
    """Return the age above which the interlocutor is considered gone."""
    return self.__max_age

ping

Store the time of a sign of life.

View Source
def ping(self) -> None:
    """Store the time of a sign of life."""
    with self.__lock:
        self.__seen = time.time()

forget

Drop the last sign of life: the interlocutor announced its end.

View Source
def forget(self) -> None:
    """Drop the last sign of life: the interlocutor announced its end."""
    with self.__lock:
        self.__seen = None

alive

Return True if the last sign of life is recent enough.

Parameters
  • max_age: (float) Override the age given at creation
Returns
  • (bool)
View Source
def alive(self, max_age: float | None=None) -> bool:
    """Return True if the last sign of life is recent enough.

        :param max_age: (float) Override the age given at creation
        :return: (bool)

        """
    if max_age is None:
        max_age = self.__max_age
    with self.__lock:
        if self.__seen is None:
            return False
        return time.time() - self.__seen < max_age

age

Return the age of the last sign of life, or None.

View Source
def age(self) -> float | None:
    """Return the age of the last sign of life, or None."""
    with self.__lock:
        if self.__seen is None:
            return None
        return time.time() - self.__seen