Make PipeHandler able handle more pipes

This commit is contained in:
Kristóf Tóth 2018-12-13 22:16:02 +01:00
parent 95673cb2c2
commit 290712d64f
1 changed files with 9 additions and 9 deletions

View File

@ -48,16 +48,18 @@ class PipeReaderThread(Thread):
class PipeHandler:
def __init__(self, pipe_path):
self._pipe_path = pipe_path
def __init__(self, *pipe_paths):
self._pipe_paths = pipe_paths
def recreate(self):
self.remove()
mkfifo(self._pipe_path)
for pipe_path in self._pipe_paths:
mkfifo(pipe_path)
def remove(self):
if exists(self._pipe_path):
remove(self._pipe_path)
for pipe_path in self._pipe_paths:
if exists(pipe_path):
remove(pipe_path)
class PipeIOServer:
@ -76,8 +78,7 @@ class PipeIOServer:
pipe_id = token_urlsafe(6)
self.in_pipe = join('/tmp', f'in_pipe_{pipe_id}')
self.out_pipe = join('/tmp', f'out_pipe_{pipe_id}')
PipeHandler(self.in_pipe).recreate()
PipeHandler(self.out_pipe).recreate()
PipeHandler(self.in_pipe, self.out_pipe).recreate()
def run(self):
for thread in self._io_threads.values():
@ -86,8 +87,7 @@ class PipeIOServer:
def stop(self):
for thread in self._io_threads.values():
thread.stop()
PipeHandler(self.in_pipe).remove()
PipeHandler(self.out_pipe).remove()
PipeHandler(self.in_pipe, self.out_pipe).remove()
if __name__ == "__main__":