-
Notifications
You must be signed in to change notification settings - Fork 64
Add session kwarg to ModelicaSystem constructor #239
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
Conversation
|
We should do something about the existing session then. It is not good to keep using the same session from two different instances. Maybe kind of transfer the ownership to ModelicaSystem so the session will be used and destroyed only by ModelicaSystem instance and will not be used by the old instance. |
|
@adeas31 Are there any other concerns with the ownership other than the If not I could update the PR removing the del method. I also realized I forgot to set the session in the case the init method was called with zero positional arguments. |
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.
|
Updated to remove the deleter from Footnotes |
|
Maybe we should not remove the I tested the following and it works fine, from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression("createModel(ExampleModel)")
from OMPython import ModelicaSystem
model = ModelicaSystem(None, "ExampleModel", session=omc)
model.buildModel()However, the following doesn't work, from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression("createModel(ExampleModel)")
from OMPython import ModelicaSystem
model = ModelicaSystem(None, "ExampleModel", session=omc)
omc.sendExpression("quit()")
model.buildModel()This gives the exception error, I guess it is expected output as the @arun3688 what do you think? |
|
I think the problem with having the del method on ModelicaSystem is that it gets called twice: once when the ModelicaSystem goes out of scope, and once when the session goes out of scope. I tried the following on the upstream master branch: from OMPython import ModelicaSystem, OMCSessionBase
deleter = OMCSessionBase.__del__
def new_deleter(self):
print("__del__:", self._omc_process.pid)
deleter(self)
OMCSessionBase.__del__ = new_deleter
model = ModelicaSystem(None, "Modelica.Blocks.Examples.PID_Controller")which gives this output showing that the deleter runs twice. Removing the deleter from ModelicaSystem fixes this. |
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.
Related Issues
Purpose
The purpose of the change is to allow usage of the high level ModelicaSystem API around models that are already loaded, or created through
createModeletc in an existing session.Approach
By adding a session keyword argument to the ModelicaSystem constructor, users can use the ModelicaSystem constructor as follows to construct a ModelicaSystem around an already loaded model from an existing session: