| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
import signal |
| 8 |
import time |
| 9 |
from multiprocessing import Process |
| 10 |
from threading import Thread |
| 11 |
|
| 12 |
from fenrirscreenreader.core import debug |
| 13 |
from fenrirscreenreader.core.eventData import FenrirEventType |
| 14 |
|
| 15 |
|
| 16 |
# Standalone functions for multiprocessing (cannot be instance methods) |
| 17 |
def _heart_beat_timer(running): |
| 18 |
""" |
| 19 |
Standalone heartbeat timer function for multiprocessing. |
| 20 |
Returns current timestamp after a short sleep. |
| 21 |
""" |
| 22 |
try: |
| 23 |
time.sleep(0.5) |
| 24 |
except Exception as e: |
| 25 |
print(f"ProcessManager _heart_beat_timer: Error during sleep: {e}") |
| 26 |
return time.time() |
| 27 |
|
| 28 |
|
| 29 |
def _custom_event_worker_process( |
| 30 |
running, event_queue, function, pargs=None, run_once=False |
| 31 |
): |
| 32 |
""" |
| 33 |
Standalone worker function for custom events in multiprocessing. |
| 34 |
Cannot use instance methods due to pickle limitations with self.env. |
| 35 |
""" |
| 36 |
if not callable(function): |
| 37 |
return |
| 38 |
while running.value: |
| 39 |
try: |
| 40 |
if pargs: |
| 41 |
function(running, event_queue, pargs) |
| 42 |
else: |
| 43 |
function(running, event_queue) |
| 44 |
except Exception as e: |
| 45 |
# Cannot use DebugManager in multiprocess context |
| 46 |
print( |
| 47 |
f"ProcessManager:_custom_event_worker_process:function(" |
| 48 |
f"{function}):{e}" |
| 49 |
) |
| 50 |
if run_once: |
| 51 |
break |
| 52 |
|
| 53 |
|
| 54 |
def _simple_event_worker_process( |
| 55 |
running, event_queue, event, function, pargs=None, run_once=False |
| 56 |
): |
| 57 |
""" |
| 58 |
Standalone worker function for simple events in multiprocessing. |
| 59 |
Cannot use instance methods due to pickle limitations with self.env. |
| 60 |
""" |
| 61 |
if not isinstance(event, FenrirEventType): |
| 62 |
return |
| 63 |
if not callable(function): |
| 64 |
return |
| 65 |
while running.value: |
| 66 |
data = None |
| 67 |
try: |
| 68 |
if pargs: |
| 69 |
data = function(running, pargs) |
| 70 |
else: |
| 71 |
data = function(running) |
| 72 |
except Exception as e: |
| 73 |
# Cannot use DebugManager in multiprocess context |
| 74 |
print( |
| 75 |
f"ProcessManager:_simple_event_worker_process:function(" |
| 76 |
f"{function}):{e}" |
| 77 |
) |
| 78 |
try: |
| 79 |
event_queue.put({"Type": event, "data": data}, timeout=0.1) |
| 80 |
except Exception as e: |
| 81 |
print(f"ProcessManager: Failed to put event to queue: {e}") |
| 82 |
if run_once: |
| 83 |
break |
| 84 |
|
| 85 |
|
| 86 |
class ProcessManager: |
| 87 |
def __init__(self): |
| 88 |
self._Processes = [] |
| 89 |
self._Threads = [] |
| 90 |
|
| 91 |
def initialize(self, environment): |
| 92 |
self.env = environment |
| 93 |
self.running = self.env["runtime"]["EventManager"].get_running() |
| 94 |
self.add_simple_event_thread( |
| 95 |
FenrirEventType.heart_beat, |
| 96 |
_heart_beat_timer, |
| 97 |
multiprocess=True, |
| 98 |
) |
| 99 |
|
| 100 |
def shutdown(self): |
| 101 |
self.terminate_all_processes() |
| 102 |
|
| 103 |
def terminate_all_processes(self): |
| 104 |
for proc in self._Processes: |
| 105 |
# try: |
| 106 |
# proc.terminate() |
| 107 |
# except KeyboardInterrupt: |
| 108 |
# pass |
| 109 |
# except: |
| 110 |
# pass |
| 111 |
proc.join(timeout=5.0) # Timeout to prevent hanging shutdown |
| 112 |
for t in self._Threads: |
| 113 |
t.join(timeout=5.0) # Timeout to prevent hanging shutdown |
| 114 |
|
| 115 |
def heart_beat_timer(self, active): |
| 116 |
try: |
| 117 |
time.sleep(0.5) |
| 118 |
except Exception as e: |
| 119 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 120 |
"ProcessManager heart_beat_timer: Error during sleep: " |
| 121 |
+ str(e), |
| 122 |
debug.DebugLevel.ERROR, |
| 123 |
) |
| 124 |
return time.time() |
| 125 |
|
| 126 |
def add_custom_event_thread( |
| 127 |
self, function, pargs=None, multiprocess=False, run_once=False |
| 128 |
): |
| 129 |
event_queue = self.env["runtime"]["EventManager"].get_event_queue() |
| 130 |
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 131 |
if multiprocess: |
| 132 |
t = Process( |
| 133 |
target=_custom_event_worker_process, |
| 134 |
args=(self.running, event_queue, function, pargs, run_once), |
| 135 |
) |
| 136 |
self._Processes.append(t) |
| 137 |
else: # use thread instead of process |
| 138 |
t = Thread( |
| 139 |
target=self.custom_event_worker_thread, |
| 140 |
args=(event_queue, function, pargs, run_once), |
| 141 |
) |
| 142 |
self._Threads.append(t) |
| 143 |
t.start() |
| 144 |
signal.signal(signal.SIGINT, original_sigint_handler) |
| 145 |
|
| 146 |
def add_simple_event_thread( |
| 147 |
self, event, function, pargs=None, multiprocess=False, run_once=False |
| 148 |
): |
| 149 |
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 150 |
if multiprocess: |
| 151 |
# Get event queue reference before creating process |
| 152 |
event_queue = self.env["runtime"]["EventManager"].get_event_queue() |
| 153 |
t = Process( |
| 154 |
target=_simple_event_worker_process, |
| 155 |
args=(self.running, event_queue, event, function, pargs, run_once), |
| 156 |
) |
| 157 |
self._Processes.append(t) |
| 158 |
else: |
| 159 |
t = Thread( |
| 160 |
target=self.simple_event_worker_thread, |
| 161 |
args=(event, function, pargs, run_once), |
| 162 |
) |
| 163 |
self._Threads.append(t) |
| 164 |
t.start() |
| 165 |
signal.signal(signal.SIGINT, original_sigint_handler) |
| 166 |
|
| 167 |
def custom_event_worker_thread( |
| 168 |
self, event_queue, function, pargs=None, run_once=False |
| 169 |
): |
| 170 |
# if not isinstance(event_queue, Queue): |
| 171 |
# return |
| 172 |
if not callable(function): |
| 173 |
return |
| 174 |
while self.running.value: |
| 175 |
try: |
| 176 |
if pargs: |
| 177 |
function(self.running, event_queue, pargs) |
| 178 |
else: |
| 179 |
function(self.running, event_queue) |
| 180 |
except Exception as e: |
| 181 |
# Cannot use DebugManager in multiprocess context due to |
| 182 |
# pickle limitations with file handles |
| 183 |
print( |
| 184 |
"ProcessManager:custom_event_worker_thread:function(" |
| 185 |
+ str(function) |
| 186 |
+ "):" |
| 187 |
+ str(e) |
| 188 |
) |
| 189 |
if run_once: |
| 190 |
break |
| 191 |
|
| 192 |
def simple_event_worker_thread( |
| 193 |
self, event, function, pargs=None, run_once=False |
| 194 |
): |
| 195 |
if not isinstance(event, FenrirEventType): |
| 196 |
return |
| 197 |
if not callable(function): |
| 198 |
return |
| 199 |
while self.running.value: |
| 200 |
data = None |
| 201 |
try: |
| 202 |
if pargs: |
| 203 |
data = function(self.running, pargs) |
| 204 |
else: |
| 205 |
data = function(self.running) |
| 206 |
except Exception as e: |
| 207 |
# Cannot use DebugManager in multiprocess context due to |
| 208 |
# pickle limitations with file handles |
| 209 |
print( |
| 210 |
"ProcessManager:simple_event_worker_thread:function(" |
| 211 |
+ str(function) |
| 212 |
+ "):" |
| 213 |
+ str(e) |
| 214 |
) |
| 215 |
self.env["runtime"]["EventManager"].put_to_event_queue(event, data) |
| 216 |
if run_once: |
| 217 |
break |