2018-12-14 16:36:53 +00:00
|
|
|
from threading import Thread
|
2018-12-14 23:32:29 +00:00
|
|
|
|
|
|
|
from .terminate_process_on_failure import terminate_process_on_failure
|
2018-12-16 20:22:20 +00:00
|
|
|
from .pipe import Pipe
|
2018-12-14 16:36:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-14 23:32:29 +00:00
|
|
|
@terminate_process_on_failure
|
2018-12-14 16:36:53 +00:00
|
|
|
def run(self):
|
|
|
|
while True:
|
2018-12-16 20:22:20 +00:00
|
|
|
message = Pipe(self._pipe_path).read()
|
2018-12-14 16:36:53 +00:00
|
|
|
if message == self._stop_sequence:
|
|
|
|
break
|
2018-12-14 23:32:29 +00:00
|
|
|
self._message_handler(message)
|
2018-12-14 16:36:53 +00:00
|
|
|
|
|
|
|
def stop(self):
|
2018-12-16 20:22:20 +00:00
|
|
|
Pipe(self._pipe_path).write(self._stop_sequence)
|
2018-12-14 16:36:53 +00:00
|
|
|
self.join()
|