Skip to content

hololinked.core.events.EventDispatcher

The actual worker which pushes the event. The separation is necessary between Event and EventDispatcher to allow class level definitions of the Event

Source code in hololinked\core\events.py
class EventDispatcher:
    """
    The actual worker which pushes the event. The separation is necessary between ``Event`` and 
    ``EventDispatcher`` to allow class level definitions of the ``Event`` 
    """

    __slots__ = ['_unique_identifier', '_unique_zmq_identifier', '_unique_http_identifier', '_publisher', '_owner_inst']

    def __init__(self, unique_identifier : str, publisher : "EventPublisher") -> None:
        self._unique_identifier = bytes(unique_identifier, encoding='utf-8')   
        self._unique_zmq_identifier = self._unique_identifier
        self._unique_http_identifier = self._unique_identifier      
        self._publisher = None
        self._owner_inst = None
        self.publisher = publisher


    @property
    def publisher(self) -> "EventPublisher": 
        """
        Event publishing PUB socket owning object.
        """
        return self._publisher

    @publisher.setter
    def publisher(self, value : "EventPublisher") -> None:
        if not self._publisher:
            self._publisher = value
        elif value != self._publisher:
            raise AttributeError("cannot reassign publisher attribute of event {}".format(self._unique_identifier)) 

    def push(self, data : typing.Any = None, *, serialize : bool = True, **kwargs) -> None:
        """
        publish the event. 

        Parameters
        ----------
        data: Any
            payload of the event
        serialize: bool, default True
            serialize the payload before pushing, set to False when supplying raw bytes. 
        **kwargs:
            zmq_clients: bool, default True
                pushes event to RPC clients, irrelevant if ``Thing`` uses only one type of serializer (refer to 
                difference between zmq_serializer and http_serializer).
            http_clients: bool, default True
                pushed event to HTTP clients, irrelevant if ``Thing`` uses only one type of serializer (refer to 
                difference between zmq_serializer and http_serializer).
        """
        self.publisher.publish(self, data, zmq_clients=kwargs.get('zmq_clients', True), 
                                http_clients=kwargs.get('http_clients', True), serialize=serialize)


    def receive_acknowledgement(self, timeout : typing.Union[float, int, None]) -> bool:
        """
        Receive acknowlegement for event receive. When the timeout argument is present and not None, 
        it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).
        """
        return self._synchronize_event.wait(timeout=timeout)

    def _set_acknowledgement(self, *args, **kwargs) -> None:
        """
        Method to be called by RPC server when an acknowledgement is received. Not for user to be set.
        """
        self._synchronize_event.set()

Attributes

publisher property writable

publisher: EventPublisher

Event publishing PUB socket owning object.

Functions

push

push(data: typing.Any = None, *, serialize: bool = True, **kwargs) -> None

publish the event.

Parameters:

Name Type Description Default

data

Any

payload of the event

None

serialize

bool

serialize the payload before pushing, set to False when supplying raw bytes.

True

**kwargs

zmq_clients: bool, default True pushes event to RPC clients, irrelevant if Thing uses only one type of serializer (refer to difference between zmq_serializer and http_serializer). http_clients: bool, default True pushed event to HTTP clients, irrelevant if Thing uses only one type of serializer (refer to difference between zmq_serializer and http_serializer).

{}
Source code in hololinked\core\events.py
def push(self, data : typing.Any = None, *, serialize : bool = True, **kwargs) -> None:
    """
    publish the event. 

    Parameters
    ----------
    data: Any
        payload of the event
    serialize: bool, default True
        serialize the payload before pushing, set to False when supplying raw bytes. 
    **kwargs:
        zmq_clients: bool, default True
            pushes event to RPC clients, irrelevant if ``Thing`` uses only one type of serializer (refer to 
            difference between zmq_serializer and http_serializer).
        http_clients: bool, default True
            pushed event to HTTP clients, irrelevant if ``Thing`` uses only one type of serializer (refer to 
            difference between zmq_serializer and http_serializer).
    """
    self.publisher.publish(self, data, zmq_clients=kwargs.get('zmq_clients', True), 
                            http_clients=kwargs.get('http_clients', True), serialize=serialize)

receive_acknowledgement

receive_acknowledgement(timeout: typing.Union[float, int, None]) -> bool

Receive acknowlegement for event receive. When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

Source code in hololinked\core\events.py
def receive_acknowledgement(self, timeout : typing.Union[float, int, None]) -> bool:
    """
    Receive acknowlegement for event receive. When the timeout argument is present and not None, 
    it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).
    """
    return self._synchronize_event.wait(timeout=timeout)