From 2856d953257fe0a7f0f043bc3e1981df7bb8dd58 Mon Sep 17 00:00:00 2001 From: Stefan Hahmann Date: Thu, 16 Oct 2025 17:42:38 +0200 Subject: [PATCH] Adapt import loading for Windows compatibility Pre-load imports in the main thread to avoid issues on Windows. --- src/appose/python_worker.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/appose/python_worker.py b/src/appose/python_worker.py index 0461f09..22109b9 100644 --- a/src/appose/python_worker.py +++ b/src/appose/python_worker.py @@ -231,6 +231,18 @@ def _process_input(self) -> None: script = request.get("script") inputs = request.get("inputs") queue = request.get("queue") + + # pre-load imports before starting a new separate Thread. + # in windows some imports (e.g. numpy) lead to the script get stuck if loaded in separate thread + block = ast.parse(script, mode='exec') + import_nodes = [ + node for node in block.body + if isinstance(node, (ast.Import, ast.ImportFrom)) + ] + import_block = ast.Module(body=import_nodes, type_ignores=[]) + compiled_imports = compile(import_block, filename="", mode="exec") + exec(compiled_imports, globals()) + task = Task(self, uuid, script, inputs) self.tasks[uuid] = task if queue == "main":