2018-12-14 16:36:53 +00:00
|
|
|
from threading import Thread
|
|
|
|
from queue import Queue
|
|
|
|
|
2018-12-14 23:32:29 +00:00
|
|
|
from .terminate_process_on_failure import terminate_process_on_failure
|
2018-12-16 20:22:20 +00:00
|
|
|
from .pipe import Pipe
|
2018-12-14 23:32:29 +00:00
|
|
|
|
2018-12-14 16:36:53 +00:00
|
|
|
|
|
|
|
class PipeWriterThread(Thread):
|
|
|
|
def __init__(self, pipe_path):
|
|
|
|
super().__init__()
|
|
|
|
self._pipe_path = pipe_path
|
|
|
|
self._write_queue = Queue()
|
|
|
|
|
|
|
|
def write(self, message):
|
|
|
|
self._write_queue.put(message, block=True)
|
|
|
|
|
2018-12-14 23:32:29 +00:00
|
|
|
@terminate_process_on_failure
|
2018-12-14 16:36:53 +00:00
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
message = self._write_queue.get(block=True)
|
|
|
|
if message is None:
|
|
|
|
break
|
2018-12-16 20:22:20 +00:00
|
|
|
Pipe(self._pipe_path).write(message)
|
2018-12-14 16:36:53 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._write_queue.put(None)
|
|
|
|
self.join()
|