diff --git a/doc/changelog.d/1026.fixed.md b/doc/changelog.d/1026.fixed.md new file mode 100644 index 000000000..7996cc7e0 --- /dev/null +++ b/doc/changelog.d/1026.fixed.md @@ -0,0 +1 @@ +Process return code \ No newline at end of file diff --git a/doc/changelog.d/1030.fixed.md b/doc/changelog.d/1030.fixed.md new file mode 100644 index 000000000..5000d5c90 --- /dev/null +++ b/doc/changelog.d/1030.fixed.md @@ -0,0 +1 @@ +Background App initialization \ No newline at end of file diff --git a/src/ansys/mechanical/core/embedding/background.py b/src/ansys/mechanical/core/embedding/background.py index d32f31636..9c2fd571f 100644 --- a/src/ansys/mechanical/core/embedding/background.py +++ b/src/ansys/mechanical/core/embedding/background.py @@ -28,6 +28,7 @@ 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 @@ -35,7 +36,6 @@ def _exit(background_app: "BackgroundApp"): """Stop the thread serving the Background App.""" background_app.stop() - atexit.unregister(_exit) class BackgroundApp: @@ -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 ) @@ -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. @@ -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 diff --git a/src/ansys/mechanical/core/embedding/initializer.py b/src/ansys/mechanical/core/embedding/initializer.py index fdf4d930f..3bb3d200d 100644 --- a/src/ansys/mechanical/core/embedding/initializer.py +++ b/src/ansys/mechanical/core/embedding/initializer.py @@ -162,24 +162,22 @@ def __check_loaded_libs(version: int = None): # pragma: no cover 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() + + 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) @@ -212,4 +210,5 @@ def initialize(version: int = None): # attach the resolver resolve(version) + INITIALIZED_VERSION = version return version diff --git a/tests/embedding/test_background.py b/tests/embedding/test_background.py index 962edf766..7ad1d7de1 100644 --- a/tests/embedding/test_background.py +++ b/tests/embedding/test_background.py @@ -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(