Refactor io thread registering

This commit is contained in:
Kristóf Tóth 2019-07-30 13:04:08 +02:00
parent 70ef9014ba
commit acd8fde005
4 changed files with 13 additions and 12 deletions

View File

@ -12,6 +12,7 @@ class PipeIOServer(PipeReaderServer, PipeWriterServer):
permissions=permissions
)
def _init_io_thread(self):
PipeReaderServer._init_io_thread(self)
PipeWriterServer._init_io_thread(self)
def _io_threads(self):
# pylint: disable=no-member
yield from PipeReaderServer._io_threads(self)
yield from PipeWriterServer._io_threads(self)

View File

@ -7,21 +7,21 @@ class PipeIOThread(Thread):
def __init__(self):
super().__init__(daemon=True)
self._stop_event = Event()
self._io_threads = []
self.__io_threads = []
@terminate_process_on_failure
def run(self):
self._init_io_thread()
for thread in self._io_threads:
self.__io_threads.extend(self._io_threads())
for thread in self.__io_threads:
thread.start()
self._stop_event.wait()
self._stop_threads()
def _init_io_thread(self):
def _io_threads(self):
raise NotImplementedError()
def _stop_threads(self):
for thread in self._io_threads:
for thread in self.__io_threads:
if thread.is_alive():
thread.stop()
self.on_stop()

View File

@ -14,13 +14,13 @@ class PipeReaderServer(PipeIOThread):
def in_pipe(self):
return self._in_pipe
def _init_io_thread(self):
def _io_threads(self):
self._reader_thread = PipeReaderThread(
self.in_pipe,
self._stop_event,
self.handle_message
)
self._io_threads.append(self._reader_thread)
yield self._reader_thread
def handle_message(self, message):
raise NotImplementedError()

View File

@ -14,12 +14,12 @@ class PipeWriterServer(PipeIOThread):
def out_pipe(self):
return self._out_pipe
def _init_io_thread(self):
def _io_threads(self):
self._writer_thread = PipeWriterThread(
self.out_pipe,
self._stop_event
)
self._io_threads.append(self._writer_thread)
yield self._writer_thread
def send_message(self, message):
self._writer_thread.write(message)