From 16177611848c028d167efc1b807f7413cc6125b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 13 May 2019 11:17:30 +0200 Subject: [PATCH] Improve CommandEventHandler error detection (avoid polling) --- lib/tfw/components/pipe_io_event_handler.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/tfw/components/pipe_io_event_handler.py b/lib/tfw/components/pipe_io_event_handler.py index 676a8e5..c0f54aa 100644 --- a/lib/tfw/components/pipe_io_event_handler.py +++ b/lib/tfw/components/pipe_io_event_handler.py @@ -6,7 +6,6 @@ from os import getpgid, killpg from os.path import join from signal import SIGTERM from secrets import token_urlsafe -from time import sleep from threading import Thread from contextlib import suppress @@ -113,26 +112,22 @@ class CommandEventHandler(PipeIOEventHandler): stdin=self._proc_stdin, stdout=self._proc_stdout, stderr=PIPE, start_new_session=True ) - self._poll_check_proc_thread = self._start_poll_check_proc() + self._monitor_proc_thread = self._start_monitor_proc() def _generate_tempfilename(self): # pylint: disable=no-self-use random_filename = partial(token_urlsafe, 10) return join('/tmp', f'{type(self).__name__}.{random_filename()}') - def _start_poll_check_proc(self): - thread = Thread(target=self._poll_check_proc, daemon=True) + def _start_monitor_proc(self): + thread = Thread(target=self._monitor_proc, daemon=True) thread.start() return thread @terminate_process_on_failure - def _poll_check_proc(self): - while True: - self.check_proc() - sleep(1) - - def check_proc(self): - if self._proc.poll() is not None: + def _monitor_proc(self): + return_code = self._proc.wait() + if return_code != 0: _, stderr = self._proc.communicate() raise RuntimeError(f'Subprocess failed: {stderr.decode()}')