From 88ef32ede3af8a9af55fce52310e5343759355ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 13 Dec 2018 23:00:54 +0100 Subject: [PATCH] Make PipeIOServer usable as a base class --- pipe_io_server.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pipe_io_server.py b/pipe_io_server.py index 9316c50..accf535 100644 --- a/pipe_io_server.py +++ b/pipe_io_server.py @@ -5,6 +5,7 @@ from os.path import exists, join from signal import signal, SIGTERM, SIGINT from secrets import token_urlsafe from collections import namedtuple +from abc import ABC, abstractmethod class PipeWriterThread(Thread): @@ -66,7 +67,7 @@ class PipeHandler: remove(pipe_path) -class PipeIOServer: +class PipeIOServer(ABC): def __init__(self, in_pipe=None, out_pipe=None): self.in_pipe, self.out_pipe = in_pipe, out_pipe self._create_pipes() @@ -85,7 +86,11 @@ class PipeIOServer: self.out_pipe = join('/tmp', f'out_pipe_{pipe_id}') PipeHandler(self.in_pipe, self.out_pipe).recreate() + @abstractmethod def _handle_message(self, message): + raise NotImplementedError() + + def send(self, message): self._io_threads.writer.write(message) def run(self): @@ -98,8 +103,13 @@ class PipeIOServer: PipeHandler(self.in_pipe, self.out_pipe).remove() +class EchoPipeIOServer(PipeIOServer): + def _handle_message(self, message): + self.send(message) + + if __name__ == "__main__": - pipe_io = PipeIOServer() + pipe_io = EchoPipeIOServer() signal(SIGTERM, lambda a, b: pipe_io.stop()) signal(SIGINT, lambda a, b: pipe_io.stop()) pipe_io.run()