Пример #1
0
def subpush_ps(shutdown, infd, outfd, rcvhwm, sndhwm):
    context = zmq.Context()

    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.RCVHWM, rcvhwm)
    socket.connect(infd)
    socket.subscribe("")  # subscribe to all topics

    out = context.socket(zmq.PUSH)
    out.setsockopt(zmq.SNDHWM, sndhwm)
    out.bind(outfd)

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)

    while not shutdown.is_set():
        target = time.time() + TIMESTEP
        if poller.poll(0):
            try:
                buf, idx = intf.recv(socket, buf=True, flags=zmq.NOBLOCK)
                intf.send(out, idx, buf=buf, flags=zmq.NOBLOCK)
            except zmq.error.Again:
                pass  # Ignore misses to send out
        missing = target - time.time()
        if missing > 0:
            time.sleep(missing)  # loop takes at minimum TIMESTEP seconds
Пример #2
0
    def recv(self, timeout=60_000):
        """Receive a package of data from the decoder pool.

        Args:
            timeout (int, optional): Timeout period in milliseconds.
            Set to None to wait forever. Defaults to 60_000.

        Raises:
            RuntimeError: Raised when method is called while a Decoder is stopped.
            TimeoutError: Raised when no messages are received in the timeout period.

        Returns:
            list: Either [arr, buf, meta, idx] or [arr, buf, idx] or [arr, meta, idx] or [arr, idx]
        """
        if self.workers == []:
            raise RuntimeError("Tried to receive frame from stopped Decoder")

        if self.receiver is None:
            self.receiver = self.context.socket(zmq.PULL)
            self.receiver.setsockopt(zmq.RCVHWM, self.outhwm)
            self.receiver.bind(self.outfd)

        if self.receiver.poll(timeout):
            return intf.recv(
                self.receiver,
                arr=True,
                buf=self.sndbuf,
                meta=self.rcvmeta,
                flags=zmq.NOBLOCK,
            )
        else:
            raise TimeoutError(
                f"No messages were received within the timeout period {timeout}ms"
            )
Пример #3
0
def pullpub_ps(shutdown, infd, outfd, rcvhwm, sndhwm):
    context = zmq.Context()

    socket = context.socket(zmq.PULL)
    socket.setsockopt(zmq.RCVHWM, rcvhwm)
    socket.bind(infd)

    out = context.socket(zmq.PUB)
    out.setsockopt(zmq.SNDHWM, sndhwm)
    out.bind(outfd)

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)

    while not shutdown.is_set():
        target = time.time() + TIMESTEP
        if poller.poll(0):
            try:
                buf, idx = intf.recv(socket, buf=True, flags=zmq.NOBLOCK)
                intf.send(out, idx, buf=buf, flags=zmq.NOBLOCK)
            except zmq.error.Again:
                pass

        missing = target - time.time()
        if missing > 0:
            time.sleep(missing)  # loop takes at minimum TIMESTEP seconds
Пример #4
0
def enc_ps(shutdown, infd, outfd, rcvhwm, sndhwm):
    context = zmq.Context()

    socket = context.socket(zmq.PULL)
    socket.setsockopt(zmq.RCVHWM, rcvhwm)
    socket.connect(infd)

    out = context.socket(zmq.PUSH)
    out.setsockopt(zmq.SNDHWM, sndhwm)
    out.connect(outfd)

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)

    encoder = partial(
        TurboJPEG().encode,
        quality=QUALITY,
        jpeg_subsample=TJSAMP_420,
        flags=TJFLAG_FASTDCT,
    )
    while not shutdown.is_set():
        target = time.time() + TIMESTEP
        if poller.poll(0):
            try:
                arr, idx = intf.recv(socket, arr=True, flags=zmq.NOBLOCK)
                intf.send(out, idx, buf=encoder(arr), flags=zmq.NOBLOCK)
            except zmq.error.Again:
                pass
        missing = target - time.time()
        if missing > 0:
            time.sleep(missing)
Пример #5
0
def dec_ps(shutdown, infd, outfd, rcvmeta, sndbuf, rcvhwm, sndhwm):
    context = zmq.Context()

    socket = context.socket(zmq.PULL)
    socket.setsockopt(zmq.RCVHWM, rcvhwm)
    socket.connect(infd)

    out = context.socket(zmq.PUSH)
    out.setsockopt(zmq.SNDHWM, sndhwm)
    out.connect(outfd)

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)

    decoder = partial(TurboJPEG().decode,
                      flags=(TJFLAG_FASTDCT + TJFLAG_FASTUPSAMPLE))
    while not shutdown.is_set():
        target = time.time() + TIMESTEP
        if poller.poll(0):
            try:
                package = intf.recv(socket,
                                    buf=True,
                                    meta=rcvmeta,
                                    flags=zmq.NOBLOCK)

                # Handle receiving meta
                if rcvmeta:
                    buf, meta, idx = package
                else:
                    buf, idx = package
                    meta = None

                arr = decoder(buf)

                # Handle forwarding buffer
                buf = buf if sndbuf else None

                intf.send(out,
                          idx,
                          arr=arr,
                          buf=buf,
                          meta=meta,
                          flags=zmq.NOBLOCK)

            except zmq.error.Again:
                pass  # ignore send misses to out.

        missing = target - time.time()
        if missing > 0:
            time.sleep(missing)