I need help with what I am trying to do.
here is my setup-
I have a database table storing config_key and value.
I read the table data into a memory tag.
I have a function written in Ignition which given me tag value for a config_key.
I use this function to assign to the variables declared in "GlobalVariables" script file in Project Library as follows-
Test = Utilities.configurationVariables.getConfigurationValues(["Test"])
I use the Variables declared in "GlobalVariables" script file throughout the project as follows-
GlobalVariables.Test
My assumption was that if I change the value in the database (through a screen) which also writes to the tag, then GlobalVariables.Test will receive the latest value wherever it is being called from as at the time of evaluation the right hand side function will be called and update the variable value and return the latest value.
But that is not happening. It retains the old value until I make some edits (Like entering a new line) in the GlobalVariables script file.
My goal was to
Not hit the database for getting config value every time
Have one place to store all the global variables (global in the sense that they are used throughout the project). Hence GlobalVariables script file.
How can I make the variables fetch the latest value once the config value is changed?
I'd appreciate help in this matter.
You cannot do this reliably via direct construction of an object in a project library script. The lifecycle of scripts is controlled by Ignition, out of your hands, and any data structure you attempt to set up will be wiped out whenever necessary changes are made.
You can use our system.util.getGlobals function to access a persistent dictionary that is created upon startup of the local JVM (the gateway, the Designer, or the Vision client) and keeps the same values around indefinitely. You must not store custom Jython classes in this dictionary - only 'plain' Jython primitives or directly imported Java classes.
You should store and retrieve from this single dictionary instance atomically using Python's builtin setdefault function.
Jython, like python, only executes modules' top level once, upon first import. Ignition script library scripts are similar to stdlib modules, and are "auto imported" upon first reference after startup or project restart.
Like python stdlib, assignments made at the outer level of an Ignition library script are persistent initializations, effectively constants. Mutable objects are commonly initialized empty, to serve as caches for other operations.
No more code will run in that script module unless functions or class methods are defined, and those are called from other Ignition events or transforms.
Jython assignments are not bindings--there is no builtin live update mechanism.
As it happens, what you are trying to do is a valuable performance optimization, such that I've created most of what you describe in my free Integration Toolkit module, via its globalVarMap() functions.