コード例 #1
0
    def test_eventloop_schedule_action_periodic(self):
        scheduler = EventLoopScheduler(exit_if_empty=False)
        gate = threading.Semaphore(0)
        period = 0.05
        counter = 3

        def action(state):
            nonlocal counter
            if state:
                counter -= 1
                return state - 1
            if counter == 0:
                gate.release()

        disp = scheduler.schedule_periodic(period, action, counter)

        def dispose(scheduler, state):
            disp.dispose()
            gate.release()

        gate.acquire()
        assert counter == 0
        assert scheduler._has_thread() is True
        scheduler.schedule(dispose)

        gate.acquire()
        assert scheduler._has_thread() is True
        sleep(period)
        scheduler.dispose()
        sleep(period)
        assert scheduler._has_thread() is False
コード例 #2
0
    def test_eventloop_schedule_periodic_dispose_error(self):
        scheduler = EventLoopScheduler(exit_if_empty=False)

        scheduler.dispose()

        ran = False

        def action(scheduler, state):
            nonlocal ran
            ran = True

        with pytest.raises(DisposedException):
            scheduler.schedule_periodic(0.1, action)

        assert ran is False
        assert scheduler._has_thread() is False
コード例 #3
0
ファイル: printer.py プロジェクト: mattj23/brother-ql-mqtt
class Printer(PrinterBase):
    def __init__(self, path: str, serial: str, check_period: int = 5):
        self.path = path
        self.serial = serial
        self.backend_cls = backend_factory("linux_kernel")['backend_class']
        self.backend = self.backend_cls(self.path)
        self.model: Optional[str] = None
        self.scheduler = EventLoopScheduler()
        self.status: Optional[Status] = None
        self.label_width: Optional[int] = None

        self.periodic = self.scheduler.schedule_periodic(timedelta(seconds=check_period), self._get_status)

    def _get_status(self, state):
        logger.debug("Getting status")

        try:
            self.status = get_status(self.backend)
            self.model = self.status.model
            self.label_width = self.status.media_width
        except:
            self.status = None

    def print_image(self, image: Image, red=False):
        raster = BrotherQLRaster(self.model)
        print_data = brother_ql.brother_ql_create.convert(raster, [image], str(self.label_width), dither=True)
        self.backend.write(print_data)
        start = time.time()
        while True:
            data = attempt_read(self.backend)
            if data:
                self.status = parse(data)
                print(self.status)

                if self.status.status_type == StatusType.ErrorOccurred:
                    logger.info(f"Error occurred while printing {self.serial}")
                    break

                if self.status.status_type == StatusType.PrintingComplete:
                    break

            time.sleep(0.2)

            if time.time() - start > 3:
                logger.info(f"Status timeout while printing on {self.serial}")
                break

        # send initialize
        self.backend.write(b'\x1b\x40')

        del raster

    def dispose(self):
        logger.debug(f"Disposing of {self.serial}")
        self.periodic.dispose()

    def info(self) -> PrinterInfo:
        return PrinterInfo(model=self.model, serial=self.serial, status=self.status)