Skip to content

Commit 40657eb

Browse files
author
Mohamed Koubaa
committed
fix sharing app instances, clarify contract
1 parent 25a6a4c commit 40657eb

File tree

1 file changed

+36
-5
lines changed
  • src/ansys/mechanical/core/embedding

1 file changed

+36
-5
lines changed

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs):
121121

122122
if BUILDING_GALLERY:
123123
if len(INSTANCES) != 0:
124-
self._app = INSTANCES[0]
125-
self._app.new()
126-
self._version = self._app.version
127-
self._disposed = True
124+
instance: App = INSTANCES[0]
125+
instance._share(self)
126+
if db_file != None:
127+
self.open(db_file)
128128
return
129129
if len(INSTANCES) > 0:
130-
raise Exception("Cannot have more than one embedded mechanical instance")
130+
raise Exception("Cannot have more than one embedded mechanical instance!")
131131
version = kwargs.get("version")
132132
self._version = initializer.initialize(version)
133133
configuration = kwargs.get("config", _get_default_addin_configuration())
@@ -288,6 +288,37 @@ def version(self):
288288
"""Returns the version of the app."""
289289
return self._version
290290

291+
def _share(self, other) -> None:
292+
"""Shares the state of self with other.
293+
294+
Other is another instance of App.
295+
This is used when the BUILDING_GALLERY flag is on.
296+
In that mode, multiple instance of App are used, but
297+
they all point to the same underlying application
298+
object. Because of that, special care needs to be
299+
taken to properly share the state. Other will be
300+
a "weak reference", which doesn't own anything."""
301+
302+
# the other app is not expecting to have a project
303+
# already loaded
304+
self._app.new()
305+
306+
# set up the type hint (typing.Self is python3.11+)
307+
other: App = other
308+
309+
# copy `self` state to other.
310+
other._app = self._app
311+
other._version = self._version
312+
other._poster = self._poster
313+
other._updated_scopes = self._updated_scopes
314+
315+
# all events will be handled by the original App instance
316+
other._subscribed = False
317+
318+
# finally, set the other disposed flag to be true
319+
# so that the shutdown sequence isn't duplicated
320+
other._disposed = True
321+
291322
def _subscribe(self):
292323
try:
293324
# This will throw an error when using pythonnet because

0 commit comments

Comments
 (0)