Gateway timer script - calling a function within a function - global not defined error

Is it possible to call a function within a function inside a Gateway timer script? I can run the same script in the script console, but in the gateway timer, I get the error “global name ‘doThis’ not defined”.

For example:

def doThis():
     *dosomething*

def startProcess(arg1):
     *dosomestuff*
     doThis

arg1 = "something"
startProcess(arg1)

I’m utilizing the threading module to split apart an internal pinging monitor (InetAddress) into separate “workers” since most of the processing is waiting for ping responses (and not cpu intensive). In the script console, I successfully took a 37 second run time to 5 seconds getting all ping response into in a dataset. I would like to run this on a Gateway Timer Script to run every so often as needed.

Gateway level, project-scoped (gateway timer, tag change etc) scripts, for complicated legacy reasons, follow legacy scoping rules.

Generally speaking, the easiest, most maintainable thing to do is immediately bail out of your gateway timer script to a function defined in a real project script, and have all of your logic there. Besides modern scoping, you also get the benefit of true diffing of project changes if it’s in a project script.

2 Likes

Great, that works!

I have a related question, I"m going to be creating separate functions for pinging different devices, would it be ok to put all of these in the same root library script and just make definitions inside there to call or should I break them apart into separate library scripts? I would be concerned about how the gateway is threading these for performance reasons. This concept would also apply to other scripts I may organize which may be more cpu intensive.

How your scripts are organized shouldn’t make any real difference to performance; put them wherever makes sense for what you’re doing and future maintenance.