Using time.sleep in a dedicated threaded gateway script

Hi, im using a gateway timer script to do some network depenedent work. If the servers connection is down this script will throw an exception.

I want to instead catch this exception and retry a couple of times with a delay.

I know using any form of sleep is kinda discouraged, but i dont see any other way of achieving this, and if it runs on its own thread it should not bog down other things?

Am i wrong here, or is there some other function that could be used?

Use two tags:

  • lastRead, memory tag, date type. Set this to system.date.now() when your script runs.
  • elapsedTime, expression tag with an expression something like
    now(4000) - {[default]lastRead
    This should run every 4 s and return a true when done.

This is reasonable thing to do in an asynchronous thread. As long as the timer event interval is much greater than the total possible retry interval, then the risk of overlap is low. If the timer event is doing nothing else, and you want more assurance that it won't overlap, then doing a limited number of sleeps in the event (with dedicated thread) is also reasonable.

1 Like