Exemplo n.º 1
0
 def preRun_(self):
     """After the fork, but before starting the process loop
     """
     super().preRun_()
     self.client = ShmemRGBClient(name=self.memname,
                                  n_ringbuffer=self.n_ringbuffer,
                                  width=self.image_dimensions[0],
                                  height=self.image_dimensions[1],
                                  mstimeout=self.mstimeout,
                                  verbose=self.verbose)
Exemplo n.º 2
0
class ValkkaShmemRGBProcess(ValkkaProcess):
    """An example process, using the valkka shared memory client for RGB images
    """

    incoming_signal_defs = {  # each key corresponds to a front- and backend methods
        "test_": {
            "test_int": int,
            "test_str": str
        },
        "stop_": [],
        "ping_": {
            "message": str
        }
    }

    outgoing_signal_defs = {"pong_o": {"message": str}}

    parameter_defs = {
        "image_dimensions": (tuple, (1920 // 4, 1080 // 4)),
        "n_ringbuffer": (int, 10),  # size of the ringbuffer
        "memname": str,
        "mstimeout": (int, 1000),
        "verbose": (bool, False)
    }

    def __init__(self, name, affinity=-1, **kwargs):
        super().__init__(name, affinity)
        # check kwargs agains parameter_defs, attach ok'd parameters to this
        # object as attributes
        parameterInitCheck(ValkkaShmemRGBProcess.parameter_defs, kwargs, self)
        typeCheck(self.image_dimensions[0], int)
        typeCheck(self.image_dimensions[1], int)

    def preRun_(self):
        """After the fork, but before starting the process loop
        """
        super().preRun_()
        self.client = ShmemRGBClient(name=self.memname,
                                     n_ringbuffer=self.n_ringbuffer,
                                     width=self.image_dimensions[0],
                                     height=self.image_dimensions[1],
                                     mstimeout=self.mstimeout,
                                     verbose=self.verbose)

    def postRun_(self):
        """Just before process exit
        """
        print(self.pre, "post: bye!")

    def cycle_(self):
        index, isize = self.client.pull()
        if (index is None):  # semaphore timed out
            print("Semaphore timeout")
        else:
            print("Current index, size=", index, isize)
            print("Payload=", self.client.shmem_list[index][0:min(isize, 10)])

    # *** backend methods corresponding to incoming signals ***

    def stop_(self):
        self.running = False

    def test_(self, test_int=0, test_str="nada"):
        print(self.pre, "test_ signal received with", test_int, test_str)

    def ping_(self, message="nada"):
        print(self.pre, "At backend: ping_ received", message,
              "sending it back to front")
        self.sendSignal_(name="pong_o", message=message)

    # ** frontend methods launching incoming signals

    def stop(self):
        self.sendSignal(name="stop_")

    def test(self, **kwargs):
        dictionaryCheck(self.incoming_signal_defs["test_"], kwargs)
        kwargs["name"] = "test_"
        self.sendSignal(**kwargs)

    def ping(self, **kwargs):
        dictionaryCheck(self.incoming_signal_defs["ping_"], kwargs)
        kwargs["name"] = "ping_"
        self.sendSignal(**kwargs)

    # ** frontend methods handling received outgoing signals ***
    def pong_o(self, message="nada"):
        print("At frontend: pong got message", message)