From ade19ea36b40ef27b43c65eb095700c7b9fd05eb Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 25 Mar 2023 22:10:00 -0600 Subject: [PATCH] impl stdout, stderr, and sigusr2 actors --- whisper-2023/hotwords.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/whisper-2023/hotwords.py b/whisper-2023/hotwords.py index 1ff0c05..130e14c 100644 --- a/whisper-2023/hotwords.py +++ b/whisper-2023/hotwords.py @@ -5,6 +5,7 @@ import queue import signal import sys from os import environ +from os import kill def main(): managerToParserQ = queue.Queue(maxsize=1) @@ -169,6 +170,12 @@ class Actor(threading.Thread): def __init__(self, inq): threading.Thread.__init__(self) self.inq = inq + self.handle = self.handle_stderr + if environ.get("STDOUT", "") == "true": + self.handle = self.handle_stdout + elif environ.get("SIGUSR2", ""): + self.pid = int(environ["SIGUSR2"]) + self.handle = self.handle_signal def run(self): log("Actor.run: start") @@ -179,8 +186,16 @@ class Actor(threading.Thread): self.handle(got[0], got[1]) log("Actor.run: stop") - def handle(self, hotword, context): - print(f"'{hotword}' in '{context}'") + def handle_stderr(self, hotword, context): + log(f"'{hotword}' in '{context}'") + + def handle_stdout(self, hotword, context): + log(context) + print(hotword) + + def handle_signal(self, hotword, context): + self.handle_stderr(hotword, context) + kill(self.pid, signal.SIGUSR2) if __name__ == "__main__": main()