Skip to content

Ensure to Pre-Load imports in main thread #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/appose/python_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ def execute_script():

self._report_completion()

# pre-load imports in 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="<imports>", mode="exec")
exec(compiled_imports, globals())

# Create a thread and save a reference to it, in case its script
# ends up killing the thread. This happens e.g. if it calls sys.exit.
self.thread = Thread(target=execute_script, name=f"Appose-{self.uuid}")
Expand Down