Terminate process if any of the IO threads fail
This commit is contained in:
parent
86662b14d8
commit
295a53a44c
@ -1,7 +1,6 @@
|
|||||||
from threading import Thread
|
from threading import Thread
|
||||||
from os import kill, getpid
|
|
||||||
from signal import SIGTERM
|
from .terminate_process_on_failure import terminate_process_on_failure
|
||||||
from traceback import print_exc
|
|
||||||
|
|
||||||
|
|
||||||
class PipeReaderThread(Thread):
|
class PipeReaderThread(Thread):
|
||||||
@ -12,17 +11,14 @@ class PipeReaderThread(Thread):
|
|||||||
self._message_handler = message_handler
|
self._message_handler = message_handler
|
||||||
self._pipe_path = pipe_path
|
self._pipe_path = pipe_path
|
||||||
|
|
||||||
|
@terminate_process_on_failure
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
with open(self._pipe_path, 'rb') as pipe:
|
with open(self._pipe_path, 'rb') as pipe:
|
||||||
message = pipe.read()
|
message = pipe.read()
|
||||||
if message == self._stop_sequence:
|
if message == self._stop_sequence:
|
||||||
break
|
break
|
||||||
try:
|
|
||||||
self._message_handler(message)
|
self._message_handler(message)
|
||||||
except: # pylint: disable=bare-except
|
|
||||||
print_exc()
|
|
||||||
kill(getpid(), SIGTERM)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
with open(self._pipe_path, 'wb') as pipe:
|
with open(self._pipe_path, 'wb') as pipe:
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
from threading import Thread
|
from threading import Thread
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
||||||
|
from .terminate_process_on_failure import terminate_process_on_failure
|
||||||
|
|
||||||
|
|
||||||
class PipeWriterThread(Thread):
|
class PipeWriterThread(Thread):
|
||||||
def __init__(self, pipe_path):
|
def __init__(self, pipe_path):
|
||||||
@ -11,6 +13,7 @@ class PipeWriterThread(Thread):
|
|||||||
def write(self, message):
|
def write(self, message):
|
||||||
self._write_queue.put(message, block=True)
|
self._write_queue.put(message, block=True)
|
||||||
|
|
||||||
|
@terminate_process_on_failure
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
message = self._write_queue.get(block=True)
|
message = self._write_queue.get(block=True)
|
||||||
|
15
pipe_io_server/terminate_process_on_failure.py
Normal file
15
pipe_io_server/terminate_process_on_failure.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from functools import wraps
|
||||||
|
from os import kill, getpid
|
||||||
|
from signal import SIGTERM
|
||||||
|
from traceback import print_exc
|
||||||
|
|
||||||
|
|
||||||
|
def terminate_process_on_failure(fun):
|
||||||
|
@wraps(fun)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return fun(*args, **kwargs)
|
||||||
|
except: # pylint: disable=bare-except
|
||||||
|
print_exc()
|
||||||
|
kill(getpid(), SIGTERM)
|
||||||
|
return wrapper
|
Loading…
Reference in New Issue
Block a user