pipe-io-server/echo_server.py

33 lines
982 B
Python
Raw Normal View History

from signal import signal, SIGTERM, SIGINT
from pipe_io_server import PipeIOServer
2019-08-06 14:16:14 +00:00
# inherit from PipeIOServer for read-write servers,
# use PipeReaderServer or PipeWriterServer for
# read-only or write-only servers
class EchoPipeIOServer(PipeIOServer):
2018-12-16 18:16:01 +00:00
def handle_message(self, message):
self.send_message(message)
2019-08-06 14:16:14 +00:00
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())
2019-05-09 11:46:31 +00:00
pipe_io.on_stop = lambda: print('Stopping...')
2019-08-06 14:16:14 +00:00
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}')
2019-08-06 14:16:14 +00:00
pipe_io.wait() # block until the server stops
if __name__ == "__main__":
main()