Skip to content
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

FIX: Background App initialization #1030

Merged
merged 11 commits into from
Jan 8, 2025
1 change: 1 addition & 0 deletions doc/changelog.d/1026.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Process return code
1 change: 1 addition & 0 deletions doc/changelog.d/1030.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Background App initialization
6 changes: 3 additions & 3 deletions src/ansys/mechanical/core/embedding/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import typing

import ansys.mechanical.core as mech
from ansys.mechanical.core.embedding import initializer
from ansys.mechanical.core.embedding.poster import Poster
import ansys.mechanical.core.embedding.utils as utils


def _exit(background_app: "BackgroundApp"):
"""Stop the thread serving the Background App."""
background_app.stop()
atexit.unregister(_exit)


class BackgroundApp:
Expand All @@ -50,6 +50,7 @@ class BackgroundApp:
def __init__(self, **kwargs):
"""Construct an instance of BackgroundApp."""
if BackgroundApp.__app_thread is None:
initializer.initialize(kwargs.get("version"))
BackgroundApp.__app_thread = threading.Thread(
target=self._start_app, kwargs=kwargs, daemon=True
)
Expand All @@ -67,8 +68,6 @@ def new():

self.post(new)

atexit.register(_exit, self)

@property
def app(self) -> mech.App:
"""Get the App instance of the background thread.
Expand Down Expand Up @@ -96,6 +95,7 @@ def stop(self) -> None:
def _start_app(self, **kwargs) -> None:
BackgroundApp.__app = mech.App(**kwargs)
BackgroundApp.__poster = BackgroundApp.__app.poster
atexit.register(_exit, self)
while True:
if BackgroundApp.__stop_signaled:
break
Expand Down
19 changes: 9 additions & 10 deletions src/ansys/mechanical/core/embedding/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,22 @@

def initialize(version: int = None):
"""Initialize Mechanical embedding."""
__check_python_interpreter_architecture() # blocks 32 bit python
__check_for_mechanical_env() # checks for mechanical-env in linux embedding

global INITIALIZED_VERSION
if version is None:
version = _get_latest_default_version()

Check warning on line 167 in src/ansys/mechanical/core/embedding/initializer.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/initializer.py#L167

Added line #L167 was not covered by tests

version = __check_for_supported_version(version=version)

if INITIALIZED_VERSION is not None:
if INITIALIZED_VERSION != version:
raise ValueError(
f"Initialized version {INITIALIZED_VERSION} "
f"does not match the expected version {version}."
)
return
return INITIALIZED_VERSION

if version is None:
version = _get_latest_default_version()

version = __check_for_supported_version(version=version)

INITIALIZED_VERSION = version
__check_python_interpreter_architecture() # blocks 32 bit python
__check_for_mechanical_env() # checks for mechanical-env in linux embedding

__set_environment(version)

Expand Down Expand Up @@ -212,4 +210,5 @@
# attach the resolver
resolve(version)

INITIALIZED_VERSION = version
return version
2 changes: 1 addition & 1 deletion tests/embedding/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _run_background_app_test(

subprocess_pass_expected = pass_expected
if pass_expected and os.name != "nt":
if int(version) < 251 or testname == "multiple_instances":
if int(version) < 251:
subprocess_pass_expected = False

process, stdout, stderr = run_subprocess(
Expand Down
Loading