Skip to content

Commit

Permalink
FIX: Background App initialization (#1030)
Browse files Browse the repository at this point in the history
Co-authored-by: Mohamed Koubaa <[email protected]>
Co-authored-by: pyansys-ci-bot <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 8, 2025
1 parent c169bc7 commit b81bef2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
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 __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)

Expand Down Expand Up @@ -212,4 +210,5 @@ def initialize(version: int = None):
# 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

0 comments on commit b81bef2

Please sign in to comment.