pipe-io-server/echo_server.py

33 lines
982 B
Python

from signal import signal, SIGTERM, SIGINT
from pipe_io_server import PipeIOServer
# inherit from PipeIOServer for read-write servers,
# use PipeReaderServer or PipeWriterServer for
# read-only or write-only servers
class EchoPipeIOServer(PipeIOServer):
def handle_message(self, message):
self.send_message(message)
def main():
pipe_io = EchoPipeIOServer('send', 'recv')
# or in case you'd prefer to do it without inheritance:
# pipe_io = PipeIOServer('send', 'recv')
# pipe_io.handle_message = pipe_io.send_message
signal(SIGTERM, lambda a, b: pipe_io.stop())
signal(SIGINT, lambda a, b: pipe_io.stop())
pipe_io.on_stop = lambda: print('Stopping...')
pipe_io.start() # start the server without blocking
print('Running pipe IO server with named pipes:')
print(f'Input: {pipe_io.in_pipe}')
print(f'Output: {pipe_io.out_pipe}')
pipe_io.wait() # block until the server stops
if __name__ == "__main__":
main()