From 4beed1056ac0b599d8b26849e41eab47d88b011d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 14 Dec 2018 15:39:18 +0100 Subject: [PATCH] Refactor example server to separate file --- echo_server.py | 18 ++++++++++++++++++ pipe_io_server.py | 16 ---------------- 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 echo_server.py diff --git a/echo_server.py b/echo_server.py new file mode 100644 index 0000000..faef8c6 --- /dev/null +++ b/echo_server.py @@ -0,0 +1,18 @@ +from signal import signal, SIGTERM, SIGINT + +from pipe_io_server import PipeIOServer + + +class EchoPipeIOServer(PipeIOServer): + def _handle_message(self, message): + self.send(message) + + +if __name__ == "__main__": + pipe_io = EchoPipeIOServer() + signal(SIGTERM, 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(f'Input: {pipe_io.in_pipe}') + print(f'Output: {pipe_io.out_pipe}') diff --git a/pipe_io_server.py b/pipe_io_server.py index accf535..8659583 100644 --- a/pipe_io_server.py +++ b/pipe_io_server.py @@ -2,7 +2,6 @@ from threading import Thread from queue import Queue from os import mkfifo, remove 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 @@ -101,18 +100,3 @@ class PipeIOServer(ABC): for thread in self._io_threads: thread.stop() PipeHandler(self.in_pipe, self.out_pipe).remove() - - -class EchoPipeIOServer(PipeIOServer): - def _handle_message(self, message): - self.send(message) - - -if __name__ == "__main__": - pipe_io = EchoPipeIOServer() - signal(SIGTERM, 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(f'Input: {pipe_io.in_pipe}') - print(f'Output: {pipe_io.out_pipe}')