Skip to content

Commit ff9a195

Browse files
authored
Merge pull request #5 from stefanhahmann/stefanhahmann-patch-2
Ensure to Pre-Load imports in main thread
2 parents c8747e3 + ce5812e commit ff9a195

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/appose/python_worker.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ def execute_script():
141141

142142
self._report_completion()
143143

144+
# pre-load imports in before starting a new separate Thread.
145+
# in windows some imports (e.g. numpy) lead to the script
146+
# get stuck if loaded in separate thread
147+
block = ast.parse(script, mode='exec')
148+
import_nodes = [
149+
node for node in block.body
150+
if isinstance(node, (ast.Import, ast.ImportFrom))
151+
]
152+
import_block = ast.Module(body=import_nodes, type_ignores=[])
153+
compiled_imports = compile(import_block, filename="<imports>", mode="exec")
154+
exec(compiled_imports, globals())
155+
144156
# Create a thread and save a reference to it, in case its script
145157
# ends up killing the thread. This happens e.g. if it calls sys.exit.
146158
self.thread = Thread(target=execute_script, name=f"Appose-{self.uuid}")

0 commit comments

Comments
 (0)