Rework PipeIOServer to use "keep pipes open" model with robust unblocking

This commit is contained in:
Kristóf Tóth 2019-03-28 16:25:53 +01:00
parent 2aa673d2c9
commit 1a28862129
5 changed files with 55 additions and 38 deletions

View File

@ -12,7 +12,7 @@ if __name__ == "__main__":
pipe_io = EchoPipeIOServer() pipe_io = EchoPipeIOServer()
signal(SIGTERM, lambda a, b: pipe_io.stop()) signal(SIGTERM, lambda a, b: pipe_io.stop())
signal(SIGINT, lambda a, b: pipe_io.stop()) signal(SIGINT, lambda a, b: pipe_io.stop())
pipe_io.run()
print('Running pipe IO server with named pipes:') print('Running pipe IO server with named pipes:')
print(f'Input: {pipe_io.in_pipe}') print(f'Input: {pipe_io.in_pipe}')
print(f'Output: {pipe_io.out_pipe}') print(f'Output: {pipe_io.out_pipe}')
pipe_io.run()

View File

@ -16,11 +16,3 @@ class Pipe:
def create(self): def create(self):
mkfifo(self.path) mkfifo(self.path)
def write(self, data):
with open(self.path, 'wb') as pipe:
pipe.write(data)
def read(self):
with open(self.path, 'rb') as pipe:
return pipe.read()

View File

@ -1,7 +1,7 @@
from os.path import join from os.path import join
from secrets import token_urlsafe from secrets import token_urlsafe
from collections import namedtuple
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from threading import Event
from .pipe_reader_thread import PipeReaderThread from .pipe_reader_thread import PipeReaderThread
from .pipe_writer_thread import PipeWriterThread from .pipe_writer_thread import PipeWriterThread
@ -11,9 +11,10 @@ from .pipe import Pipe
class PipeIOServer(ABC): class PipeIOServer(ABC):
def __init__(self, in_pipe=None, out_pipe=None): def __init__(self, in_pipe=None, out_pipe=None):
self._in_pipe, self._out_pipe = in_pipe, out_pipe self._in_pipe, self._out_pipe = in_pipe, out_pipe
self._io_threads = None
self._create_pipes() 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): def _create_pipes(self):
if not self.in_pipe or not self.out_pipe: if not self.in_pipe or not self.out_pipe:
@ -31,28 +32,30 @@ class PipeIOServer(ABC):
def out_pipe(self): def out_pipe(self):
return self._out_pipe return self._out_pipe
def _init_io_threads(self): def _create_io_threads(self):
io_threads_dict = { reader_thread = PipeReaderThread(self.in_pipe, self._stop_event, self.handle_message)
'reader': PipeReaderThread(self.in_pipe, self.handle_message), writer_thread = PipeWriterThread(self.out_pipe, self._stop_event)
'writer': PipeWriterThread(self.out_pipe) return reader_thread, writer_thread
}
IOThreadsTuple = namedtuple('IOThreadsTuple', sorted(io_threads_dict.keys()))
self._io_threads = IOThreadsTuple(**io_threads_dict)
@abstractmethod @abstractmethod
def handle_message(self, message): def handle_message(self, message):
raise NotImplementedError() raise NotImplementedError()
def send(self, message): def send(self, message):
self._io_threads.writer.write(message) self._writer_thread.write(message)
def run(self): def run(self):
for thread in self._io_threads: for thread in self._io_threads:
thread.start() thread.start()
self._stop_event.wait()
self._stop()
def stop(self): def stop(self):
self._stop_event.set()
def _stop(self):
for thread in self._io_threads: for thread in self._io_threads:
if thread.isAlive(): if thread.is_alive():
thread.stop() thread.stop()
Pipe(self.in_pipe).remove() Pipe(self.in_pipe).remove()
Pipe(self.out_pipe).remove() Pipe(self.out_pipe).remove()

View File

@ -1,25 +1,37 @@
from contextlib import suppress
from os import open as osopen
from os import write, close, O_WRONLY, O_NONBLOCK
from threading import Thread from threading import Thread
from .terminate_process_on_failure import terminate_process_on_failure from .terminate_process_on_failure import terminate_process_on_failure
from .pipe import Pipe
class PipeReaderThread(Thread): class PipeReaderThread(Thread):
_stop_sequence = b'stop_reading' eof = ''
stop_sequence = 'stop_reading'
def __init__(self, pipe_path, message_handler): def __init__(self, pipe_path, stop_event, message_handler):
super().__init__() super().__init__()
self._message_handler = message_handler self._message_handler = message_handler
self._pipe_path = pipe_path self._pipe_path = pipe_path
self._stop_event = stop_event
@terminate_process_on_failure @terminate_process_on_failure
def run(self): def run(self):
while True: with open(self._pipe_path, 'r') as pipe:
message = Pipe(self._pipe_path).read() while True:
if message == self._stop_sequence: message = pipe.readline().rstrip()
break if message in (self.eof, self.stop_sequence):
self._message_handler(message) self._stop_event.set()
break
self._message_handler(message)
def stop(self): def stop(self):
Pipe(self._pipe_path).write(self._stop_sequence) self.unblock()
self.join() self.join()
def unblock(self):
with suppress(OSError):
fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK)
write(fd, f'{self.stop_sequence}\n'.encode())
close(fd)

View File

@ -2,13 +2,13 @@ from threading import Thread
from queue import Queue from queue import Queue
from .terminate_process_on_failure import terminate_process_on_failure from .terminate_process_on_failure import terminate_process_on_failure
from .pipe import Pipe
class PipeWriterThread(Thread): class PipeWriterThread(Thread):
def __init__(self, pipe_path): def __init__(self, pipe_path, stop_event):
super().__init__() super().__init__()
self._pipe_path = pipe_path self._pipe_path = pipe_path
self._stop_event = stop_event
self._write_queue = Queue() self._write_queue = Queue()
def write(self, message): def write(self, message):
@ -16,12 +16,22 @@ class PipeWriterThread(Thread):
@terminate_process_on_failure @terminate_process_on_failure
def run(self): def run(self):
while True: try:
message = self._write_queue.get(block=True) with open(self._pipe_path, 'w') as pipe:
if message is None: while True:
break message = self._write_queue.get(block=True)
Pipe(self._pipe_path).write(message) if message is None:
self._stop_event.set()
break
pipe.write(f'{message}\n')
pipe.flush()
except BrokenPipeError:
self._stop_event.set()
def stop(self): def stop(self):
self._write_queue.put(None) self.unblock()
self.join() self.join()
def unblock(self):
self._write_queue.put(None)
open(self._pipe_path, 'r').close()