2019-07-29 16:15:19 +00:00
|
|
|
from threading import Thread, Event
|
|
|
|
|
|
|
|
from .terminate_process_on_failure import terminate_process_on_failure
|
|
|
|
|
|
|
|
|
|
|
|
class PipeIOThread(Thread):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(daemon=True)
|
|
|
|
self._stop_event = Event()
|
2019-07-30 11:04:08 +00:00
|
|
|
self.__io_threads = []
|
2019-07-29 16:15:19 +00:00
|
|
|
|
|
|
|
@terminate_process_on_failure
|
|
|
|
def run(self):
|
2019-07-30 11:04:08 +00:00
|
|
|
self.__io_threads.extend(self._io_threads())
|
|
|
|
for thread in self.__io_threads:
|
2019-07-29 16:15:19 +00:00
|
|
|
thread.start()
|
|
|
|
self._stop_event.wait()
|
|
|
|
self._stop_threads()
|
|
|
|
|
2019-07-30 11:04:08 +00:00
|
|
|
def _io_threads(self):
|
2019-07-29 16:15:19 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def _stop_threads(self):
|
2019-07-30 11:04:08 +00:00
|
|
|
for thread in self.__io_threads:
|
2019-07-29 16:15:19 +00:00
|
|
|
if thread.is_alive():
|
|
|
|
thread.stop()
|
|
|
|
self.on_stop()
|
|
|
|
|
|
|
|
def on_stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._stop_event.set()
|
|
|
|
if self.is_alive():
|
|
|
|
self.join()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
self._stop_event.wait()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.start()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type_, value, tb):
|
|
|
|
self.stop()
|