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 sharing app instances, clarify contract #784

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/changelog.d/784.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix sharing app instances, clarify contract
41 changes: 36 additions & 5 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@

if BUILDING_GALLERY:
if len(INSTANCES) != 0:
self._app = INSTANCES[0]
self._app.new()
self._version = self._app.version
self._disposed = True
instance: App = INSTANCES[0]
instance._share(self)
if db_file != None:
self.open(db_file)

Check warning on line 127 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L124-L127

Added lines #L124 - L127 were not covered by tests
return
if len(INSTANCES) > 0:
raise Exception("Cannot have more than one embedded mechanical instance")
raise Exception("Cannot have more than one embedded mechanical instance!")

Check warning on line 130 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L130

Added line #L130 was not covered by tests
version = kwargs.get("version")
self._version = initializer.initialize(version)
configuration = kwargs.get("config", _get_default_addin_configuration())
Expand Down Expand Up @@ -288,6 +288,37 @@
"""Returns the version of the app."""
return self._version

def _share(self, other) -> None:
"""Shares the state of self with other.

Other is another instance of App.
This is used when the BUILDING_GALLERY flag is on.
In that mode, multiple instance of App are used, but
they all point to the same underlying application
object. Because of that, special care needs to be
taken to properly share the state. Other will be
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they all point to the same underlying application object. As a result, you need to be careful to properly share the state.

a "weak reference", which doesn't own anything.
"""
# the other app is not expecting to have a project
# already loaded
self.new()

Check warning on line 304 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L304

Added line #L304 was not covered by tests

# set up the type hint (typing.Self is python3.11+)
other: App = other

Check warning on line 307 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L307

Added line #L307 was not covered by tests

# copy `self` state to other.
other._app = self._app
other._version = self._version
other._poster = self._poster
other._updated_scopes = self._updated_scopes

Check warning on line 313 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L310-L313

Added lines #L310 - L313 were not covered by tests

# all events will be handled by the original App instance
other._subscribed = False

Check warning on line 316 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L316

Added line #L316 was not covered by tests

# finally, set the other disposed flag to be true
# so that the shutdown sequence isn't duplicated
other._disposed = True

Check warning on line 320 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L320

Added line #L320 was not covered by tests

def _subscribe(self):
try:
# This will throw an error when using pythonnet because
Expand Down
Loading