improve termination of threads

This commit is contained in:
chrys 2018-03-25 16:05:10 +02:00
parent f01583b13c
commit d2cc7192b3
4 changed files with 15 additions and 8 deletions

View File

@ -6,7 +6,7 @@
from fenrirscreenreader.core import debug from fenrirscreenreader.core import debug
from fenrirscreenreader.core.eventData import fenrirEventType from fenrirscreenreader.core.eventData import fenrirEventType
import time import time, signal
from threading import Thread from threading import Thread
from multiprocessing import Process from multiprocessing import Process
@ -25,8 +25,13 @@ class processManager():
for proc in self._Processes: for proc in self._Processes:
try: try:
proc.terminate() proc.terminate()
except Exception as e: except KeyboardInterrupt:
print(e) pass
except:
pass
proc.join()
for t in self._Threads:
t.join()
def heartBeatTimer(self, active): def heartBeatTimer(self, active):
try: try:
time.sleep(0.5) time.sleep(0.5)
@ -35,6 +40,7 @@ class processManager():
return time.time() return time.time()
def addCustomEventThread(self, function, pargs = None, multiprocess = False, runOnce = False): def addCustomEventThread(self, function, pargs = None, multiprocess = False, runOnce = False):
eventQueue = self.env['runtime']['eventManager'].getEventQueue() eventQueue = self.env['runtime']['eventManager'].getEventQueue()
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
if multiprocess: if multiprocess:
t = Process(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce)) t = Process(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._Processes.append(t) self._Processes.append(t)
@ -42,8 +48,9 @@ class processManager():
t = Thread(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce)) t = Thread(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._Threads.append(t) self._Threads.append(t)
t.start() t.start()
signal.signal(signal.SIGINT, original_sigint_handler)
def addSimpleEventThread(self, event, function, pargs = None, multiprocess = False, runOnce = False): def addSimpleEventThread(self, event, function, pargs = None, multiprocess = False, runOnce = False):
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
if multiprocess: if multiprocess:
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce)) t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._Processes.append(t) self._Processes.append(t)
@ -51,7 +58,7 @@ class processManager():
t = Thread(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce)) t = Thread(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._Threads.append(t) self._Threads.append(t)
t.start() t.start()
signal.signal(signal.SIGINT, original_sigint_handler)
def customEventWorkerThread(self, eventQueue, function, pargs = None, runOnce = False): def customEventWorkerThread(self, eventQueue, function, pargs = None, runOnce = False):
#if not isinstance(eventQueue, Queue): #if not isinstance(eventQueue, Queue):
# return # return

View File

@ -34,7 +34,7 @@ class driver(screenDriver):
self.hichar = None self.hichar = None
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog) self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog, multiprocess=True)
def getCurrScreen(self): def getCurrScreen(self):
self.env['screen']['oldTTY'] = self.env['screen']['newTTY'] self.env['screen']['oldTTY'] = self.env['screen']['newTTY']
try: try: