Rework PipeIOServer to use "keep pipes open" model with robust unblocking
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from os.path import join
|
||||
from secrets import token_urlsafe
|
||||
from collections import namedtuple
|
||||
from abc import ABC, abstractmethod
|
||||
from threading import Event
|
||||
|
||||
from .pipe_reader_thread import PipeReaderThread
|
||||
from .pipe_writer_thread import PipeWriterThread
|
||||
@@ -11,9 +11,10 @@ from .pipe import Pipe
|
||||
class PipeIOServer(ABC):
|
||||
def __init__(self, in_pipe=None, out_pipe=None):
|
||||
self._in_pipe, self._out_pipe = in_pipe, out_pipe
|
||||
self._io_threads = None
|
||||
self._create_pipes()
|
||||
self._init_io_threads()
|
||||
self._stop_event = Event()
|
||||
self._reader_thread, self._writer_thread = self._create_io_threads()
|
||||
self._io_threads = (self._reader_thread, self._writer_thread)
|
||||
|
||||
def _create_pipes(self):
|
||||
if not self.in_pipe or not self.out_pipe:
|
||||
@@ -31,28 +32,30 @@ class PipeIOServer(ABC):
|
||||
def out_pipe(self):
|
||||
return self._out_pipe
|
||||
|
||||
def _init_io_threads(self):
|
||||
io_threads_dict = {
|
||||
'reader': PipeReaderThread(self.in_pipe, self.handle_message),
|
||||
'writer': PipeWriterThread(self.out_pipe)
|
||||
}
|
||||
IOThreadsTuple = namedtuple('IOThreadsTuple', sorted(io_threads_dict.keys()))
|
||||
self._io_threads = IOThreadsTuple(**io_threads_dict)
|
||||
def _create_io_threads(self):
|
||||
reader_thread = PipeReaderThread(self.in_pipe, self._stop_event, self.handle_message)
|
||||
writer_thread = PipeWriterThread(self.out_pipe, self._stop_event)
|
||||
return reader_thread, writer_thread
|
||||
|
||||
@abstractmethod
|
||||
def handle_message(self, message):
|
||||
raise NotImplementedError()
|
||||
|
||||
def send(self, message):
|
||||
self._io_threads.writer.write(message)
|
||||
self._writer_thread.write(message)
|
||||
|
||||
def run(self):
|
||||
for thread in self._io_threads:
|
||||
thread.start()
|
||||
self._stop_event.wait()
|
||||
self._stop()
|
||||
|
||||
def stop(self):
|
||||
self._stop_event.set()
|
||||
|
||||
def _stop(self):
|
||||
for thread in self._io_threads:
|
||||
if thread.isAlive():
|
||||
if thread.is_alive():
|
||||
thread.stop()
|
||||
Pipe(self.in_pipe).remove()
|
||||
Pipe(self.out_pipe).remove()
|
||||
|
Reference in New Issue
Block a user