2019-04-10 10:40:51 +00:00
|
|
|
from threading import Thread, Event
|
2018-12-13 21:06:13 +00:00
|
|
|
|
2018-12-14 16:36:53 +00:00
|
|
|
from .pipe_reader_thread import PipeReaderThread
|
|
|
|
from .pipe_writer_thread import PipeWriterThread
|
2018-12-16 20:22:20 +00:00
|
|
|
from .pipe import Pipe
|
2019-04-10 10:40:51 +00:00
|
|
|
from .terminate_process_on_failure import terminate_process_on_failure
|
2018-12-13 21:06:13 +00:00
|
|
|
|
|
|
|
|
2019-06-23 16:22:04 +00:00
|
|
|
class PipeIOServer(Thread):
|
2019-06-23 18:53:24 +00:00
|
|
|
def __init__(self, in_pipe, out_pipe, permissions=0o600):
|
2019-05-07 15:16:43 +00:00
|
|
|
super().__init__(daemon=True)
|
2018-12-16 22:28:33 +00:00
|
|
|
self._in_pipe, self._out_pipe = in_pipe, out_pipe
|
2019-05-04 19:06:00 +00:00
|
|
|
self._create_pipes(permissions)
|
2019-03-28 15:25:53 +00:00
|
|
|
self._stop_event = Event()
|
2019-06-23 16:22:04 +00:00
|
|
|
self._reader_thread, self._writer_thread = None, None
|
|
|
|
self._io_threads = None
|
2018-12-13 21:06:13 +00:00
|
|
|
|
2019-05-04 19:06:00 +00:00
|
|
|
def _create_pipes(self, permissions):
|
|
|
|
Pipe(self.in_pipe).recreate(permissions)
|
|
|
|
Pipe(self.out_pipe).recreate(permissions)
|
2018-12-13 21:06:13 +00:00
|
|
|
|
2018-12-16 22:28:33 +00:00
|
|
|
@property
|
|
|
|
def in_pipe(self):
|
|
|
|
return self._in_pipe
|
|
|
|
|
|
|
|
@property
|
|
|
|
def out_pipe(self):
|
|
|
|
return self._out_pipe
|
|
|
|
|
2018-12-16 18:16:01 +00:00
|
|
|
def handle_message(self, message):
|
2018-12-13 22:00:54 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2019-04-17 12:10:47 +00:00
|
|
|
def send_message(self, message):
|
2019-03-28 15:25:53 +00:00
|
|
|
self._writer_thread.write(message)
|
2018-12-13 21:44:46 +00:00
|
|
|
|
2019-04-10 10:40:51 +00:00
|
|
|
@terminate_process_on_failure
|
2018-12-13 21:06:13 +00:00
|
|
|
def run(self):
|
2019-06-23 16:22:04 +00:00
|
|
|
self._init_io_threads()
|
2018-12-13 21:44:46 +00:00
|
|
|
for thread in self._io_threads:
|
2018-12-13 21:06:13 +00:00
|
|
|
thread.start()
|
2019-03-28 15:25:53 +00:00
|
|
|
self._stop_event.wait()
|
2019-05-09 13:26:39 +00:00
|
|
|
self._stop_threads()
|
2018-12-13 21:06:13 +00:00
|
|
|
|
2019-06-23 16:22:04 +00:00
|
|
|
def _init_io_threads(self):
|
|
|
|
self._reader_thread = PipeReaderThread(
|
|
|
|
self.in_pipe,
|
|
|
|
self._stop_event,
|
|
|
|
self.handle_message
|
|
|
|
)
|
|
|
|
self._writer_thread = PipeWriterThread(
|
|
|
|
self.out_pipe,
|
|
|
|
self._stop_event
|
|
|
|
)
|
|
|
|
self._io_threads = (self._reader_thread, self._writer_thread)
|
|
|
|
|
2018-12-13 21:06:13 +00:00
|
|
|
def stop(self):
|
2019-03-28 15:25:53 +00:00
|
|
|
self._stop_event.set()
|
2019-04-10 10:40:51 +00:00
|
|
|
if self.is_alive():
|
|
|
|
self.join()
|
2019-03-28 15:25:53 +00:00
|
|
|
|
2019-05-09 13:26:39 +00:00
|
|
|
def _stop_threads(self):
|
2018-12-13 21:44:46 +00:00
|
|
|
for thread in self._io_threads:
|
2019-03-28 15:25:53 +00:00
|
|
|
if thread.is_alive():
|
2018-12-15 00:07:02 +00:00
|
|
|
thread.stop()
|
2018-12-16 20:22:20 +00:00
|
|
|
Pipe(self.in_pipe).remove()
|
|
|
|
Pipe(self.out_pipe).remove()
|
2019-06-23 16:44:34 +00:00
|
|
|
self.on_stop()
|
2019-05-09 11:46:31 +00:00
|
|
|
|
2019-06-23 16:44:34 +00:00
|
|
|
def on_stop(self):
|
|
|
|
pass
|
2019-05-09 11:26:48 +00:00
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
self._stop_event.wait()
|
2019-06-23 16:40:06 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.start()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type_, value, tb):
|
|
|
|
self.stop()
|