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 subprocess import run
from time import sleep
from signal import SIGRTMIN, SIGUSR1, sigtimedwait
from signal import SIGRTMIN, SIGUSR1, sigtimedwait, sigwait
class Module:
# All text that the module contains. Will be re-generated every time the
@ -105,18 +105,15 @@ class Module:
break
sleep(self.refresh)
else:
sig = sigtimedwait([self.signal], self.refresh)
if sig:
print("Recieved signal " + str(sig))
continue
if self.refresh == 0:
sigwait([self.signal])
else:
sig = sigtimedwait([self.signal], self.refresh)
if sig:
print("Recieved signal " + str(sig))
continue
def start_thread(self):
thread = Thread(target=self.thread_callback)
thread.start()
self.__ident = thread.ident
def get_ident(self):
if self.__ident:
return self.__ident
else:
return None
self.ident = thread.ident

View file

@ -1,13 +1,13 @@
from signal import SIGRTMIN, SIGTERM, pthread_kill, signal
from sys import exit
from threading import enumerate as th_enumerate
from signal import pthread_kill
from signal import signal as sigsignal
class SignalRouter:
def term(self, sig, frame):
print("Term signal received! Safely closing threads!")
for thread in th_enumerate():
pthread_kill(thread.ident, SIGTERM)
def __route_signal(self, signal, frame):
pthread_kill(self.signal_thread_dictionary[signal], signal)
def __init__(self):
signal(SIGTERM, self.term)
signal(SIGRTMIN, self.rtmin)
self.signal_thread_dictionary = {}
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)
bar.add_module(module)
module.start_thread()
if module.signal:
signal_router.register_signal_thread(module.signal, module.ident)