Global keywod in project scripting

I have a Vision project script that has declared a number of dictionaries using the python global keyword like below. I have read that it is not advised to do this in Ignition. The intent of this is to interact locally with a device and use the dictionaries to store results/send commands from within multiple areas of the project. If using the global keyword is not advised how would this be accomplished differently?

global CommandResponse, CommandRequest, Status, Control, InspectionResults, InspectionParameters

#Global kvs dictionaries

CommandResponse = {}
CommandRequest = {}
Status = {}
InspectionResults = {}
InspectionParameters = {}

Use system.util.getGlobals/system.util.globals to store persistent data across the entire lifetime of the local process (e.g. the local Vision client or Designer instance, or with great care the gateway itself).

Be very careful to only store 'native' Python or Java data structures.

Storing user-defined Jython classes into the globals dictionary leads to memory leaks of the entire Jython interpreter, which will rapidly run your applications out of memory as we create and destroy script managers in the background.

2 Likes

The global keyword doesn't do what you think. And is not necessary for what you are trying to do. Variables declared at the outermost level of a project library script are automatically global to the project and persist between project saves.

3 Likes

That’s what I thought, so you could get rid of the first line and just go with the dictionary declaration that follows and that would be “global” to the project correct. The data stored would only persist as long as the client is open correct. The code was done by someone else and I’m trying to understand why the global keyword is needed.

At the top level, the global keyword is a no-op. The global keywork is intended to be used within functions (at the beginning) to make assignment to them effective instead of shadowed. Please study the python docs for this.

Paul I thought the system.util.global was for having gateway globals. In this case we just want to have a project only variable that is accessed from anywhere in the project. I’ll read the manual again.