Refactor example server to separate file

This commit is contained in:
Kristóf Tóth 2018-12-14 15:39:18 +01:00
parent 88ef32ede3
commit 4beed1056a
2 changed files with 18 additions and 16 deletions

18
echo_server.py Normal file
View File

@ -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}')

View File

@ -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}')