pipe-io-server/pipe_io_server/pipe_writer_thread.py

28 lines
708 B
Python
Raw Normal View History

from threading import Thread
from queue import Queue
from .terminate_process_on_failure import terminate_process_on_failure
from .pipe import Pipe
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)
@terminate_process_on_failure
def run(self):
while True:
message = self._write_queue.get(block=True)
if message is None:
break
Pipe(self._pipe_path).write(message)
def stop(self):
self._write_queue.put(None)
self.join()