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 permissions=permissions
) )
def _init_io_thread(self): def _io_threads(self):
PipeReaderServer._init_io_thread(self) # pylint: disable=no-member
PipeWriterServer._init_io_thread(self) yield from PipeReaderServer._io_threads(self)
yield from PipeWriterServer._io_threads(self)

View File

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

View File

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

View File

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