pipe-io-server/pipe_io_server/pipe_reader_thread.py
2018-12-16 21:22:20 +01:00

26 lines
694 B
Python

from threading import Thread
from .terminate_process_on_failure import terminate_process_on_failure
from .pipe import Pipe
class PipeReaderThread(Thread):
_stop_sequence = b'stop_reading'
def __init__(self, pipe_path, message_handler):
super().__init__()
self._message_handler = message_handler
self._pipe_path = pipe_path
@terminate_process_on_failure
def run(self):
while True:
message = Pipe(self._pipe_path).read()
if message == self._stop_sequence:
break
self._message_handler(message)
def stop(self):
Pipe(self._pipe_path).write(self._stop_sequence)
self.join()