Configuring the scanRateResolution
The scanRateResolution setting controls the frequency at which the main loop of a script for a Thing executes in the LSR. Once it is started, a script enters into a loop that executes until the script resource is shut down. Each iteration of this main loop takes a number of actions, potentially increasing CPU usage.
At a high level, a script takes the following actions:
1. Goes through all your configured properties. If the scanRate amount of time for each property has expired, the property is evaluated to check whether it should be pushed. To evaluate the property, the LSR calls the read method of the property handler for each property. The new value is compared to the old (if necessary). If it needs to be pushed to the ThingWorx instance, the property and its value are added to a temporary list of properties to be pushed.
2. The temporary list of properties is pushed to the ThingWorx instance. If no properties have been evaluated, or no properties need to be pushed, this list is empty and nothing is pushed.
3. The registration of the Thing with the EMS is checked, using a call to the EMS.
4. If the taskRate time has expired, configured tasks are executed.
5. GC (garbage collection) is run if five seconds or more have elapsed.
6. The thread then sleeps for the number of milliseconds specified in the scanRateResolution parameter.
If you do not need the main loop to drive calls to the handlers that read your properties, you could set your scanRateResolution fairly high. A high setting would cause the main loop to sleep longer between iterations, which could have a few side effects:
1. The registration check will happen at the scanRateResolution, unless you adjust the keepAliveRate to be greater than the scanRateResolution.
2. You will need to set your taskRate and scanRate parameters to be greater than the scanRateResolution, or the script resource will complain during startup. Since it controls the pause of the main loop, the scanRateResolution is the main limiting factor in how often the main loop actions occur.
3. Shutting down the script resource can be delayed by up to the number of milliseconds specified for the scanRateResolution parameter, since the main loop must exit for the script to shut down.
The default value for the scanRateResolution is 500 milliseconds. If however, you do not require the loop to execute that often, consider setting this value much higher, even 10,000 milliseconds or more, to slow the execution of the loop and save CPU load.