summaryrefslogtreecommitdiff
path: root/src/tox/execute/local_sub_process/read_via_thread.py
blob: ba9029be46512b81ee5cb13b25446414c227f89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
A reader that drain a stream via its file no on a background thread.
"""
from __future__ import annotations

from abc import ABC, abstractmethod
from threading import Event, Thread
from types import TracebackType
from typing import Callable

WAIT_GENERAL = 0.05  # stop thread join every so often (give chance to a signal interrupt)


class ReadViaThread(ABC):
    def __init__(self, file_no: int, handler: Callable[[bytes], None], name: str, drain: bool) -> None:
        self.file_no = file_no
        self.stop = Event()
        self.thread = Thread(target=self._read_stream, name=f"tox-r-{name}-{file_no}")
        self.handler = handler
        self._on_exit_drain = drain

    def __enter__(self) -> ReadViaThread:
        self.thread.start()
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,  # noqa: U100
        exc_val: BaseException | None,  # noqa: U100
        exc_tb: TracebackType | None,  # noqa: U100
    ) -> None:
        self.stop.set()  # signal thread to stop
        while self.thread.is_alive():  # wait until it stops
            self.thread.join(WAIT_GENERAL)
        self._drain_stream()  # read anything left

    @abstractmethod
    def _read_stream(self) -> None:
        raise NotImplementedError

    @abstractmethod
    def _drain_stream(self) -> None:
        raise NotImplementedError