From acd8fde0050d5947c9e91e158bf67990bd94081b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 30 Jul 2019 13:04:08 +0200 Subject: [PATCH] Refactor io thread registering --- pipe_io_server/pipe_io_server.py | 7 ++++--- pipe_io_server/pipe_io_thread.py | 10 +++++----- pipe_io_server/pipe_reader_server.py | 4 ++-- pipe_io_server/pipe_writer_server.py | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pipe_io_server/pipe_io_server.py b/pipe_io_server/pipe_io_server.py index 02fff8b..8d6dada 100644 --- a/pipe_io_server/pipe_io_server.py +++ b/pipe_io_server/pipe_io_server.py @@ -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) diff --git a/pipe_io_server/pipe_io_thread.py b/pipe_io_server/pipe_io_thread.py index 868cb79..91a50b4 100644 --- a/pipe_io_server/pipe_io_thread.py +++ b/pipe_io_server/pipe_io_thread.py @@ -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() diff --git a/pipe_io_server/pipe_reader_server.py b/pipe_io_server/pipe_reader_server.py index c0b8312..952d9f6 100644 --- a/pipe_io_server/pipe_reader_server.py +++ b/pipe_io_server/pipe_reader_server.py @@ -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() diff --git a/pipe_io_server/pipe_writer_server.py b/pipe_io_server/pipe_writer_server.py index 5c483ca..f155c26 100644 --- a/pipe_io_server/pipe_writer_server.py +++ b/pipe_io_server/pipe_writer_server.py @@ -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)