Refactor pipe logic to a separate class

This commit is contained in:
Kristóf Tóth
2018-12-16 21:22:20 +01:00
parent a389273779
commit d28ea355f7
4 changed files with 37 additions and 25 deletions

View File

@ -1,26 +1,11 @@
from os import mkfifo, remove
from os.path import exists, join
from os.path import join
from secrets import token_urlsafe
from collections import namedtuple
from abc import ABC, abstractmethod
from .pipe_reader_thread import PipeReaderThread
from .pipe_writer_thread import PipeWriterThread
class PipeHandler:
def __init__(self, *pipe_paths):
self._pipe_paths = pipe_paths
def recreate(self):
self.remove()
for pipe_path in self._pipe_paths:
mkfifo(pipe_path)
def remove(self):
for pipe_path in self._pipe_paths:
if exists(pipe_path):
remove(pipe_path)
from .pipe import Pipe
class PipeIOServer(ABC):
@ -35,7 +20,8 @@ class PipeIOServer(ABC):
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, self.out_pipe).recreate()
Pipe(self.in_pipe).recreate()
Pipe(self.out_pipe).recreate()
def _init_io_threads(self):
io_threads_dict = {
@ -60,4 +46,5 @@ class PipeIOServer(ABC):
for thread in self._io_threads:
if thread.isAlive():
thread.stop()
PipeHandler(self.in_pipe, self.out_pipe).remove()
Pipe(self.in_pipe).remove()
Pipe(self.out_pipe).remove()