implemented SignalRouter; signal functionality complete
This commit is contained in:
parent
ebe2e2137e
commit
2ee1d09918
3 changed files with 20 additions and 21 deletions
|
@ -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
|
||||
|
@ -104,6 +104,9 @@ class Module:
|
|||
if self.refresh == 0:
|
||||
break
|
||||
sleep(self.refresh)
|
||||
else:
|
||||
if self.refresh == 0:
|
||||
sigwait([self.signal])
|
||||
else:
|
||||
sig = sigtimedwait([self.signal], self.refresh)
|
||||
if sig:
|
||||
|
@ -113,10 +116,4 @@ class Module:
|
|||
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue