# OPC Disconnects and long read/write periods

**URL:** <https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [October 21, 2022, 6:14pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545 "2022-10-21T18:14:42Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 21, 2022, 9:26pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/21 "2022-10-21T21:26:54Z")

</div>

> [@Tyler\_Earl](#):
>
> Triggering it 10 times gave me these execution times.
> 
> 1. 1s 444ms
> 2. 18s 855ms
> 3. 2s 865 ms
> 4. 930 ms
> 5. 650ms
> 6. 1s 130ms
> 7. 22s 900ms
> 8. 12s 370ms
> 9. 13s 120ms
> 10. 5s 825ms

These times are atrocious for a system that should be updating every 800ms. I watched the threads status page and I don't even see this script being called. I imagine that is because it is executing so quickly....once it executed. It's like the valueChange isn't executing right away because the opc read isn't able to acquire the data at the 800ms poll rate. It doesn't see the value update right away and therefore the script isnt called.

---

<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:** [October 21, 2022, 9:30pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/22 "2022-10-21T21:30:25Z")

</div>

Ooooo! Do you have many tag change event scripts? Any with long executions? Or any form of `sleep()` function calls? You might be backlogging the tag event thread pool. Instead of using a tag event (on the tag), try a gateway tag change event (in the project's scripting/events part of the designer tree). Those don't have the same kind of queuing issues.

(Set it to have a dedicated thread just to be sure.)

If that makes a difference (and I suspect it will), you will need to visit all of your tags to get rid of the pathological algorithms.

(If you are using `sleep()` anywhere in Ignition, you are usually screwing up.)

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 22, 2022, 6:39pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/23 "2022-10-22T18:39:42Z")

</div>

> [@pturmel](#):
>
> Ooooo! Do you have many tag change event scripts? Any with long executions? Or any form of `sleep()` function calls?

Oooh this is interesting. I didn't code the system initially so I can't attest to how many tags are using the valueChange event, but I do believe there are many. One thing I noticed is the tag values seem to stutter in the tag editor window. Like the tags are just straight up not being updated. Would the backlog of change events cause the opc connection to stop updating tags as well?

---

<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:** [October 23, 2022, 12:54am UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/24 "2022-10-23T00:54:24Z")

</div>

> [@Tyler\_Earl](#):
>
> Would the backlog of change events cause the opc connection to stop updating tags as well?

Generally not, but you are on a buggy version of Ignition. 🤷‍♂️

Try my experiment first.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 23, 2022, 1:07am UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/25 "2022-10-23T01:07:02Z")

</div>

> [@pturmel](#):
>
> Generally not, but you are on a buggy version of Ignition. 🤷‍♂️
> 
> Try my experiment first.

Sure thing. I will follow up Monday with the results. Thank you and everyone else chiming in with advice.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 23, 2022, 7:13pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/26 "2022-10-23T19:13:57Z")

</div>

> [@pturmel](#):
>
> Try my experiment first.

Forgive me for what is probably a beginner mistake, but I tried to setup a gateway event and I'm having zero luck getting it to work.

I have the tag being processed as the normal gMES\_Debug, i checked its path and its valid.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/e/7eaa7a8036b2f223ace330bb69517b00f4508600.png)

Here is the script I have executing.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/1/713484bb26ca722f104e1c4032e4a6cdf976ca21.png)

I attempted to put the logger in just to see if it was actually being called. I'm not sure where the logger data can be viewed.

---

<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:** [October 23, 2022, 7:15pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/27 "2022-10-23T19:15:55Z")

</div>

> [@Tyler\_Earl](#):
>
> I'm not sure where the logger data can be viewed.

Gateway status page for gateway-scoped scripts.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 23, 2022, 7:22pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/28 "2022-10-23T19:22:14Z")

</div>

> [@pturmel](#):
>
> Gateway status page for gateway-scoped scripts.

Yeah that's where I was watching. Doesn't appear to be calling the script. Not sure why, the value is certainly changing.

---

<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:** [October 23, 2022, 7:25pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/29 "2022-10-23T19:25:05Z")

</div>

Look for some other error coming from a syntax error or similar--those show up just once at the time you save the project.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 23, 2022, 7:57pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/30 "2022-10-23T19:57:03Z")

</div>

> [@pturmel](#):
>
> those show up just once at the time you save the project.

Maybe this is my problem. At what point does the script become active? I assumed it was when I click ok on the gateway event menu. Is it only active once the project is saved?

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [October 23, 2022, 8:36pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/31 "2022-10-23T20:36:12Z")

</div>

Project script edits you've OK'd become active when project is saved. The gateway tag change event scripts you setup under project tree are part of the project (unlike tag event scripts defined directly on tags).

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 23, 2022, 9:29pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/32 "2022-10-23T21:29:44Z")

</div>

This was the problem. I wasn't saving the project. I would attempt it without saving it and it didn't work so I disabled the script so as to not cause any unforeseen issues.

It is working now, I did the echo test 10 times...

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/d/3da3118c24fd5f1e3a57784c05bcb0a6324c1fed.png)

These response times seem by far more consistent. However, the plant isn't running at capacity right now because its the weekend and therefore ignition isnt loaded down with its other tasks. I will run the test again tomorrow while I am there and we are at fully operation.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 24, 2022, 12:46am UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/33 "2022-10-24T00:46:34Z")

</div>

Are there any white papers or documentation on best practices on when you should use tag level valueChange scripts vs gateway level valueChange scripts?

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [October 24, 2022, 1:27am UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/34 "2022-10-24T01:27:25Z")

</div>

Not a white paper, but this thread has some good info (don't stop at first post):

> [@Difference Gateway Event vs Tag Event Scripts](https://forum.inductiveautomation.com/t/difference-gateway-event-vs-tag-event-scripts/63191):
>
> I have already seen some posts on this topic, but none of them answered my questions. I want to monitor tag changes and react to them. This monitoring has to be robust (It is a heartbeat. I added some margin and can handle some lost events, but if I regularly loose events this will cause alarms all the time) Now there are 2 ways to do this: Tag Change scripts in Gateway Events I really like this solution because all the scripts are in the project scope. They are all in one place, easily ma…

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 24, 2022, 10:58pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/35 "2022-10-24T22:58:51Z")

</div>

> [@pturmel](#):
>
> Try my experiment first.

So I was able to move the scripts related to omron over to the gateway events and it helped tremendously. I am going to move the problematic scripts for the Siemens side tomorrow. I'm hoping to see the same drastic improvements.

I'm still having a hard time understanding why it was problematic using the tag events. By default, it looks like tags all each have their own queue (3 I believe). Was it a problem with the tag scripts queueing up in that, or a problem some where else? I'd like to better understand why this change helped so much.

---

<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:** [October 24, 2022, 11:03pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/36 "2022-10-24T23:03:50Z")

</div>

> [@Tyler\_Earl](#):
>
> all each have their own queue (3 I believe).

The each have their own queue of length 5. But there are only three threads servicing all of the queues. If any event script bogs down, only two threads will be servicing everything else. If three event scripts bog down, nothing gets serviced.

Do note that if you move long-duration event scripts to the unbounded queues, you can still get in trouble. (Out of memory, in particular.) Plan on rewriting those algorithms.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 24, 2022, 11:26pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/37 "2022-10-24T23:26:33Z")

</div>

> [@pturmel](#):
>
> Plan on rewriting those algorithms.

Is there a way to view the script threads to see which script is causing the hang ups? That way I can target those scripts first.

---

<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:** [October 24, 2022, 11:46pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/38 "2022-10-24T23:46:31Z")

</div>

Not really, no. Export all of your tags and search that for the literal `sleep(`.

After you've fixed those, add uses of `java.lang.System.nanoTime()` to capture start and end timestamps. Log the delta at the end of every event. Skip logging for anything shorter than five milliseconds.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 24, 2022, 11:54pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/39 "2022-10-24T23:54:46Z")

</div>

> [@pturmel](#):
>
> Log the delta at the end of every event.

You're the man! I really appreciate all of the advice you've provided. I will post with results.

---

<div class="post-metadata">

**Author:** ![Tyler\_Earl](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Tyler\_Earl](https://forum.inductiveautomation.com/u/Tyler_Earl)\
**Post date:** [October 25, 2022, 9:18pm UTC](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545/40 "2022-10-25T21:18:27Z")

</div>

So I've identified a script that is taking in excess of 30s to run. Sometimes takes over a minute. There are 4 tags that call the script. I frequently see the script occupying all 3 of the available threads. I'm thinking this may be the problem.

While I investigate reworking these scripts to make them more efficient, I was thinking about increasing the number of threads available for tag event processing. If I were to increase it from 3 to say 5, it would allow one thread to always be available even if the other 4 get backed up processing the lengthy script. This would be a temporary solution to help our lack of throughput through our process. Run with 5 threads to help get parts out while I rework the problematic script. Once the script is figured out I would change the number of threads back to 3.

My question with this is if I increase the thread count to 5 does it pull resources from else where that might create a problem in itself?

[Previous page](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545.md?page=1)

[Next page](https://forum.inductiveautomation.com/t/opc-disconnects-and-long-read-write-periods/65545.md?page=3)
