Skip to content

Commit 32d5de3

Browse files
committed
Add session kwarg to ModelicaSystem constructor
If the session kwarg is specified, the ModelicaSystem will use that session instead of creating a new one. This is useful for creating a ModelicaSystem for a model that is already loaded in an already existing session. Remove __del__ method from ModelicaSystem. Having the deleter there meant that the deleter for the session would run multiple times if a session had shared ownership. The deleter will still run when the session's reference count reaches zero, or when it is garbage collected.
1 parent 5368216 commit 32d5de3

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

OMPython/__init__.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ class ModelicaSystem(object):
821821
def __init__(self, fileName=None, modelName=None, lmodel=None,
822822
useCorba=False, commandLineOptions=None,
823823
variableFilter=None, customBuildDirectory=None, verbose=True, raiseerrors=False,
824-
omhome: str = None): # 1
824+
omhome: str = None, session: OMCSessionBase = None): # 1
825825
"""
826826
"constructor"
827827
It initializes to load file and build a model, generating object, exe, xml, mat, and json files. etc. It can be called :
@@ -831,11 +831,14 @@ def __init__(self, fileName=None, modelName=None, lmodel=None,
831831
Note: If the model file is not in the current working directory, then the path where file is located must be included together with file name. Besides, if the Modelica model contains several different models within the same package, then in order to build the specific model, in second argument, user must put the package name with dot(.) followed by specific model name.
832832
ex: myModel = ModelicaSystem("ModelicaModel.mo", "modelName")
833833
"""
834+
if session is not None:
835+
self.getconn = session
836+
elif useCorba:
837+
self.getconn = OMCSession(omhome=omhome)
838+
else:
839+
self.getconn = OMCSessionZMQ(omhome=omhome)
840+
834841
if fileName is None and modelName is None and not lmodel: # all None
835-
if useCorba:
836-
self.getconn = OMCSession(omhome=omhome)
837-
else:
838-
self.getconn = OMCSessionZMQ(omhome=omhome)
839842
return
840843

841844
self.tree = None
@@ -857,11 +860,6 @@ def __init__(self, fileName=None, modelName=None, lmodel=None,
857860

858861
self._verbose = verbose
859862

860-
if useCorba:
861-
self.getconn = OMCSession(omhome=omhome)
862-
else:
863-
self.getconn = OMCSessionZMQ(omhome=omhome)
864-
865863
## needed for properly deleting the OMCSessionZMQ
866864
self._omc_log_file = self.getconn._omc_log_file
867865
self._omc_process = self.getconn._omc_process
@@ -906,9 +904,6 @@ def __init__(self, fileName=None, modelName=None, lmodel=None,
906904

907905
self.buildModel(variableFilter)
908906

909-
def __del__(self):
910-
OMCSessionBase.__del__(self)
911-
912907
def setCommandLineOptions(self, commandLineOptions: str):
913908
## set commandLineOptions if provided by users
914909
if commandLineOptions is not None:

0 commit comments

Comments
 (0)