Operate on a byte level instead of utf8 strings only

This commit is contained in:
Kristóf Tóth 2019-04-01 14:27:37 +02:00
parent 3a2ccc1ea4
commit 07c35cc8bf
2 changed files with 7 additions and 7 deletions

View File

@ -7,8 +7,8 @@ from .terminate_process_on_failure import terminate_process_on_failure
class PipeReaderThread(Thread): class PipeReaderThread(Thread):
eof = '' eof = b''
stop_sequence = 'stop_reading' stop_sequence = b'stop_reading'
def __init__(self, pipe_path, stop_event, message_handler): def __init__(self, pipe_path, stop_event, message_handler):
super().__init__() super().__init__()
@ -18,7 +18,7 @@ class PipeReaderThread(Thread):
@terminate_process_on_failure @terminate_process_on_failure
def run(self): def run(self):
with open(self._pipe_path, 'r') as pipe: with open(self._pipe_path, 'rb') as pipe:
while True: while True:
message = pipe.readline().rstrip() message = pipe.readline().rstrip()
if message in (self.eof, self.stop_sequence): if message in (self.eof, self.stop_sequence):
@ -33,5 +33,5 @@ class PipeReaderThread(Thread):
def unblock(self): def unblock(self):
with suppress(OSError): with suppress(OSError):
fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK) fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK)
write(fd, f'{self.stop_sequence}\n'.encode()) write(fd, self.stop_sequence + b'\n')
close(fd) close(fd)

View File

@ -17,13 +17,13 @@ class PipeWriterThread(Thread):
@terminate_process_on_failure @terminate_process_on_failure
def run(self): def run(self):
try: try:
with open(self._pipe_path, 'w') as pipe: with open(self._pipe_path, 'wb') as pipe:
while True: while True:
message = self._write_queue.get(block=True) message = self._write_queue.get(block=True)
if message is None: if message is None:
self._stop_event.set() self._stop_event.set()
break break
pipe.write(f'{message}\n') pipe.write(message + b'\n')
pipe.flush() pipe.flush()
except BrokenPipeError: except BrokenPipeError:
self._stop_event.set() self._stop_event.set()
@ -34,4 +34,4 @@ class PipeWriterThread(Thread):
def unblock(self): def unblock(self):
self._write_queue.put(None) self._write_queue.put(None)
open(self._pipe_path, 'r').close() open(self._pipe_path, 'rb').close()