implemented SignalRouter; signal functionality complete

This commit is contained in:
Noah Swerhun 2023-03-14 20:00:08 -05:00
parent ebe2e2137e
commit 2ee1d09918
3 changed files with 20 additions and 21 deletions

View file

@ -1,7 +1,7 @@
from threading import Thread from threading import Thread
from subprocess import run from subprocess import run
from time import sleep from time import sleep
from signal import SIGRTMIN, SIGUSR1, sigtimedwait from signal import SIGRTMIN, SIGUSR1, sigtimedwait, sigwait
class Module: class Module:
# All text that the module contains. Will be re-generated every time the # All text that the module contains. Will be re-generated every time the
@ -104,6 +104,9 @@ class Module:
if self.refresh == 0: if self.refresh == 0:
break break
sleep(self.refresh) sleep(self.refresh)
else:
if self.refresh == 0:
sigwait([self.signal])
else: else:
sig = sigtimedwait([self.signal], self.refresh) sig = sigtimedwait([self.signal], self.refresh)
if sig: if sig:
@ -113,10 +116,4 @@ class Module:
def start_thread(self): def start_thread(self):
thread = Thread(target=self.thread_callback) thread = Thread(target=self.thread_callback)
thread.start() thread.start()
self.__ident = thread.ident self.ident = thread.ident
def get_ident(self):
if self.__ident:
return self.__ident
else:
return None

View file

@ -1,13 +1,13 @@
from signal import SIGRTMIN, SIGTERM, pthread_kill, signal from signal import pthread_kill
from sys import exit from signal import signal as sigsignal
from threading import enumerate as th_enumerate
class SignalRouter: class SignalRouter:
def term(self, sig, frame): def __route_signal(self, signal, frame):
print("Term signal received! Safely closing threads!") pthread_kill(self.signal_thread_dictionary[signal], signal)
for thread in th_enumerate():
pthread_kill(thread.ident, SIGTERM)
def __init__(self): def __init__(self):
signal(SIGTERM, self.term) self.signal_thread_dictionary = {}
signal(SIGRTMIN, self.rtmin)
def register_signal_thread(self, signal, thread_ident):
self.signal_thread_dictionary[signal] = thread_ident
sigsignal(signal, self.__route_signal)

View file

@ -21,6 +21,8 @@ def main():
module = Module(module_config, padding, bar) module = Module(module_config, padding, bar)
bar.add_module(module) bar.add_module(module)
module.start_thread() module.start_thread()
if module.signal:
signal_router.register_signal_thread(module.signal, module.ident)