From d2cc7192b34d52219fa96558875126c9ab003af4 Mon Sep 17 00:00:00 2001 From: chrys Date: Sun, 25 Mar 2018 16:05:10 +0200 Subject: [PATCH] improve termination of threads --- src/fenrirscreenreader/core/fenrirManager.py | 2 +- src/fenrirscreenreader/core/processManager.py | 17 ++++++++++++----- .../screenDriver/ptyDriver.py | 2 +- .../screenDriver/vcsaDriver.py | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/fenrirscreenreader/core/fenrirManager.py b/src/fenrirscreenreader/core/fenrirManager.py index 90af1c46..c8c5ba66 100644 --- a/src/fenrirscreenreader/core/fenrirManager.py +++ b/src/fenrirscreenreader/core/fenrirManager.py @@ -19,7 +19,7 @@ class fenrirManager(): if not cliArgs: return try: - self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs, self) + self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs, self) if not self.environment: raise RuntimeError('Cannot Initialize. Maybe the configfile is not available or not parseable') except RuntimeError: diff --git a/src/fenrirscreenreader/core/processManager.py b/src/fenrirscreenreader/core/processManager.py index 4617cdcb..bc1af5a0 100644 --- a/src/fenrirscreenreader/core/processManager.py +++ b/src/fenrirscreenreader/core/processManager.py @@ -6,7 +6,7 @@ from fenrirscreenreader.core import debug from fenrirscreenreader.core.eventData import fenrirEventType -import time +import time, signal from threading import Thread from multiprocessing import Process @@ -25,8 +25,13 @@ class processManager(): for proc in self._Processes: try: proc.terminate() - except Exception as e: - print(e) + except KeyboardInterrupt: + pass + except: + pass + proc.join() + for t in self._Threads: + t.join() def heartBeatTimer(self, active): try: time.sleep(0.5) @@ -35,6 +40,7 @@ class processManager(): return time.time() def addCustomEventThread(self, function, pargs = None, multiprocess = False, runOnce = False): eventQueue = self.env['runtime']['eventManager'].getEventQueue() + original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) if multiprocess: t = Process(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce)) self._Processes.append(t) @@ -42,8 +48,9 @@ class processManager(): t = Thread(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce)) self._Threads.append(t) t.start() - + signal.signal(signal.SIGINT, original_sigint_handler) def addSimpleEventThread(self, event, function, pargs = None, multiprocess = False, runOnce = False): + original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) if multiprocess: t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce)) self._Processes.append(t) @@ -51,7 +58,7 @@ class processManager(): t = Thread(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce)) self._Threads.append(t) t.start() - + signal.signal(signal.SIGINT, original_sigint_handler) def customEventWorkerThread(self, eventQueue, function, pargs = None, runOnce = False): #if not isinstance(eventQueue, Queue): # return diff --git a/src/fenrirscreenreader/screenDriver/ptyDriver.py b/src/fenrirscreenreader/screenDriver/ptyDriver.py index ef52985a..1b1512e9 100644 --- a/src/fenrirscreenreader/screenDriver/ptyDriver.py +++ b/src/fenrirscreenreader/screenDriver/ptyDriver.py @@ -57,7 +57,7 @@ class driver(screenDriver): signal.signal(signal.SIGWINCH, self.handleSigwinch) def initialize(self, environment): self.env = environment - self.env['runtime']['processManager'].addCustomEventThread(self.terminalEmulation) + self.env['runtime']['processManager'].addCustomEventThread(self.terminalEmulation) def getCurrScreen(self): self.env['screen']['oldTTY'] = '1' self.env['screen']['newTTY'] = '1' diff --git a/src/fenrirscreenreader/screenDriver/vcsaDriver.py b/src/fenrirscreenreader/screenDriver/vcsaDriver.py index 66fad262..4719dd82 100644 --- a/src/fenrirscreenreader/screenDriver/vcsaDriver.py +++ b/src/fenrirscreenreader/screenDriver/vcsaDriver.py @@ -34,7 +34,7 @@ class driver(screenDriver): self.hichar = None def initialize(self, environment): self.env = environment - self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog) + self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog, multiprocess=True) def getCurrScreen(self): self.env['screen']['oldTTY'] = self.env['screen']['newTTY'] try: