Attempt to fix cannot pickle 'TextIOWrapper' instances error for some distros.

This commit is contained in:
Storm Dragon
2025-11-23 18:29:38 -05:00
parent 2f3a114790
commit 77a3aae5a4
2 changed files with 73 additions and 11 deletions

View File

@@ -13,6 +13,64 @@ from fenrirscreenreader.core import debug
from fenrirscreenreader.core.eventData import FenrirEventType from fenrirscreenreader.core.eventData import FenrirEventType
# Standalone worker functions for multiprocessing (cannot be instance methods)
def _custom_event_worker_process(
running, event_queue, function, pargs=None, run_once=False
):
"""
Standalone worker function for custom events in multiprocessing.
Cannot use instance methods due to pickle limitations with self.env.
"""
if not callable(function):
return
while running.value:
try:
if pargs:
function(running, event_queue, pargs)
else:
function(running, event_queue)
except Exception as e:
# Cannot use DebugManager in multiprocess context
print(
f"ProcessManager:_custom_event_worker_process:function("
f"{function}):{e}"
)
if run_once:
break
def _simple_event_worker_process(
running, event_queue, event, function, pargs=None, run_once=False
):
"""
Standalone worker function for simple events in multiprocessing.
Cannot use instance methods due to pickle limitations with self.env.
"""
if not isinstance(event, FenrirEventType):
return
if not callable(function):
return
while running.value:
data = None
try:
if pargs:
data = function(running, pargs)
else:
data = function(running)
except Exception as e:
# Cannot use DebugManager in multiprocess context
print(
f"ProcessManager:_simple_event_worker_process:function("
f"{function}):{e}"
)
try:
event_queue.put({"Type": event, "data": data}, timeout=0.1)
except Exception as e:
print(f"ProcessManager: Failed to put event to queue: {e}")
if run_once:
break
class ProcessManager: class ProcessManager:
def __init__(self): def __init__(self):
self._Processes = [] self._Processes = []
@@ -60,8 +118,8 @@ class ProcessManager:
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
if multiprocess: if multiprocess:
t = Process( t = Process(
target=self.custom_event_worker_thread, target=_custom_event_worker_process,
args=(event_queue, function, pargs, run_once), args=(self.running, event_queue, function, pargs, run_once),
) )
self._Processes.append(t) self._Processes.append(t)
else: # use thread instead of process else: # use thread instead of process
@@ -78,9 +136,11 @@ class ProcessManager:
): ):
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
if multiprocess: if multiprocess:
# Get event queue reference before creating process
event_queue = self.env["runtime"]["EventManager"].get_event_queue()
t = Process( t = Process(
target=self.simple_event_worker_thread, target=_simple_event_worker_process,
args=(event, function, pargs, run_once), args=(self.running, event_queue, event, function, pargs, run_once),
) )
self._Processes.append(t) self._Processes.append(t)
else: else:
@@ -106,12 +166,13 @@ class ProcessManager:
else: else:
function(self.running, event_queue) function(self.running, event_queue)
except Exception as e: except Exception as e:
self.env["runtime"]["DebugManager"].write_debug_out( # Cannot use DebugManager in multiprocess context due to
# pickle limitations with file handles
print(
"ProcessManager:custom_event_worker_thread:function(" "ProcessManager:custom_event_worker_thread:function("
+ str(function) + str(function)
+ "):" + "):"
+ str(e), + str(e)
debug.DebugLevel.ERROR,
) )
if run_once: if run_once:
break break
@@ -131,12 +192,13 @@ class ProcessManager:
else: else:
data = function(self.running) data = function(self.running)
except Exception as e: except Exception as e:
self.env["runtime"]["DebugManager"].write_debug_out( # Cannot use DebugManager in multiprocess context due to
# pickle limitations with file handles
print(
"ProcessManager:simple_event_worker_thread:function(" "ProcessManager:simple_event_worker_thread:function("
+ str(function) + str(function)
+ "):" + "):"
+ str(e), + str(e)
debug.DebugLevel.ERROR,
) )
self.env["runtime"]["EventManager"].put_to_event_queue(event, data) self.env["runtime"]["EventManager"].put_to_event_queue(event, data)
if run_once: if run_once:

View File

@@ -4,5 +4,5 @@
# Fenrir TTY screen reader # Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributors. # By Chrys, Storm Dragon, and contributors.
version = "2025.10.17" version = "2025.11.23"
code_name = "testing" code_name = "testing"