DIAdem Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
JuliaDawkins

Allow multiple embedded python engines at the same time

Status: Completed

The Python command del solves this problem.

Currently, ONE python engine is started with each instance of DIAdem.  This behavior is not well-documented and comes with limitations. 

 

For example, if I have an imported function ".py" script called "MyHelperFunctions.py" and import this into another script, the function is only loaded once.  If I make edits to this function, and save the file, the edits will not be available until I close/reopen DIAdem!

 

I suggest having multiple embedded python engines at the same time.  And this would enable DIAdem to always be ready for a python call but still retire a python engine that was bloated.

 

At the least, having a script command to spin up a new python engine would be very helpful for me!

8 Comments
JuliaDawkins
Member

Ok, now I see I can edit imbedded functions but I need to "run" the script first (I was just saving them before), so that removes one limitation! 

 

However, I still cannot remove definitions until I restart DIAdem.  For VBS, I can use ScriptCmdReset to clear all global variables and definitions and there doesn't seem to be a way to do this in Python DIAdem.

Walter_Rick
NI Employee (retired)
Status changed to: New

With importlib.reload you can reload an already loaded Python module. Possibly this is what you are looking for.

 

import importlib

importlib.reload(MyPyLib)

 

Greetings

Walter

 

JuliaDawkins
Member

Not quite! If I have a custom function that is defined in a python *.py file, and I import this into another script, but then decide I don't need it, is there a way to remove this definition without restarting DIAdem?

 

Related, variables declared in Python scripts seem to behave as "global" variables.  If I create a variable:

 

a = 5

 

And then run this script, in a new script I can execute:

 

print(a)

 

without defining "a" in the current script! This is very different behavior than VBS and most Python script-writing programs.

 

For VBS, I can use ScriptCmdReset to clear all global variables and definitions and there doesn't seem to be a way to do this in Python DIAdem without restarting DIAdem.

Walter_Rick
NI Employee (retired)

In this case you can del the module like

 

del MyPyLib

JuliaDawkins
Member

That doesn't work for me.  

 

I have a python script I ran in DIAdem and saved as, "MyHelperFunctions.PY".  It contains the following code:

 

def ScaleValue2X(scale, value):
    print(scale*value)

 

I created a different, new python script with the following code and ran it:

 

import MyHelperFunctions

ScaleValue2X(3,15)

It prints the value as I expect.

 

Now, I change the above script to the following and run it:

 

del MyHelperFunctions

 

However, if I run the following code in a new script, it still recognizes the function. And it does not seem to be removed from the python engine.

ScaleValue2X(3,30)

 

The "del" command does seem to work for variables, though I still think it is odd behavior that python variables are global for the DIAdem environment.

 

Walter_Rick
NI Employee (retired)

This works for me.

 

Helper script function: Scale

def ScaleValue2X(scale, value):
    print(scale*value)

 

Main script 1

import Scale

Scale.ScaleValue2X(2,3)

del Scale

Scale.ScaleValue2X(2,3)

The secon call throws an error as expected

 

Main script 2

Scale.ScaleValue2X(2,3)

Throws an error as expected.

I am using Python 3.9

 

 

 

 

JuliaDawkins
Member

I'm using Python 3.8 and that also works for me in a new instance of DIAdem, but I really think it didn't last time! 🙂 

Walter_Rick
NI Employee (retired)
Status changed to: Completed

The Python command del solves this problem.