# Using time.sleep in a dedicated threaded gateway script

**URL:** <https://forum.inductiveautomation.com/t/using-time-sleep-in-a-dedicated-threaded-gateway-script/77850>\
**Category:** Ignition\
**Created:** [August 15, 2023, 8:25am UTC](https://forum.inductiveautomation.com/t/using-time-sleep-in-a-dedicated-threaded-gateway-script/77850 "2023-08-15T08:25:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eskil\_Holum](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@Eskil\_Holum](https://forum.inductiveautomation.com/u/Eskil_Holum)\
**Post date:** [August 15, 2023, 8:25am UTC](https://forum.inductiveautomation.com/t/using-time-sleep-in-a-dedicated-threaded-gateway-script/77850/1 "2023-08-15T08:25:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [August 15, 2023, 11:29am UTC](https://forum.inductiveautomation.com/t/using-time-sleep-in-a-dedicated-threaded-gateway-script/77850/2 "2023-08-15T11:29:48Z")

</div>

Use two tags:

- `lastRead`, memory tag, date type. Set this to [`system.date.now()`](https://docs.inductiveautomation.com/display/DOC81/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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 15, 2023, 11:57am UTC](https://forum.inductiveautomation.com/t/using-time-sleep-in-a-dedicated-threaded-gateway-script/77850/3 "2023-08-15T11:57:49Z")

</div>

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.
