# Tags writing and reading in a script

**URL:** <https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611>\
**Category:** Ignition\
**Created:** [May 11, 2020, 12:01am UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611 "2020-05-11T00:01:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![NII](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nii/32/12920_2.png) [@NII](https://forum.inductiveautomation.com/u/NII)\
**Post date:** [May 11, 2020, 12:01am UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/1 "2020-05-11T00:01:06Z")

</div>

If I have a series of system.tag.writeBlocking and/or system.tag.readBlocking in a script, does the function blocks the execution of the script until it completes the current write?

In reading, will it hang if it cannot complete the read or gets no response?

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [May 11, 2020, 12:49am UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/2 "2020-05-11T00:49:50Z")

</div>

Yes if you have a script like the following:

```auto
Print 1
System.tag.writeBlocking([tagPath], [value])
Print 2

```

The “Print 2” will not execute until writeBlocking is complete

You can set this timeout with the third parameter

```auto
System.tag.writeBlocking([tagPath], [value], timeout)

```

If you want to not block the script AT ALL, then you could use writeAsync, which allows you to send the call asyncronously, and supply a callback function when the write completes, incase you need to do something else based on the outcome of the tag write.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 11, 2020, 1:47am UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/3 "2020-05-11T01:47:23Z")

</div>

To add, if you call this from the gui e.g. a button click or event handler script, and the read/write takes a while, the gui will be frozen for that time

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 11, 2020, 12:51pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/4 "2020-05-11T12:51:19Z")

</div>

.

> [@NII](#):
>
> In reading, will it hang if it cannot complete the read or gets no response?

> [@The Manual](#):
>
> Integer timeout - [optional] How long to wait in milliseconds before the read operation times out. This parameter is optional, and **defaults to 45000 milliseconds** if not specified.

---

<div class="post-metadata">

**Author:** ![NII](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nii/32/12920_2.png) [@NII](https://forum.inductiveautomation.com/u/NII)\
**Post date:** [May 11, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/5 "2020-05-11T15:21:27Z")

</div>

Does it also apply to writing/reading to memory tags? If yes, can I assume it’ll block only for a very short time, uSec range (system latency) ?

Thanks for all the responses.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 11, 2020, 3:24pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/6 "2020-05-11T15:24:20Z")

</div>

You are correct. It applies no matter the tag type.

---

<div class="post-metadata">

**Author:** ![maja](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@maja](https://forum.inductiveautomation.com/u/maja)\
**Post date:** [July 14, 2020, 6:43pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/7 "2020-07-14T18:43:50Z")

</div>

If I have ScriptA, ScriptB, Tag1, Tag2 and Tag3. On value change of Tag1 executing ScriptA in ScriptA writing with blocking valut to Tag3. On value change of Tag2 executing ScriptB in ScriptB writing with blocking valut to Tag3. What if happen that ScriptA and ScriptB executing in same time (or overlap in one moment) will writing value to Tag3 wait that previous script finish?

---

<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:** [July 14, 2020, 9:05pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/8 "2020-07-14T21:05:33Z")

</div>

> [@maja](#):
>
> will writing value to Tag3 wait that previous script finish?

No. You have race that depends on random timing.

---

<div class="post-metadata">

**Author:** ![maja](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@maja](https://forum.inductiveautomation.com/u/maja)\
**Post date:** [July 14, 2020, 9:17pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/9 "2020-07-14T21:17:50Z")

</div>

Thanks!

What if I call from tag value change project script (same from Tag1 and Tag2), project script for each call will executed in a separate thread or will wait that previous execution finish?

---

<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:** [July 14, 2020, 9:20pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/10 "2020-07-14T21:20:24Z")

</div>

Undetermined. Tag scripting uses a thread pool, so multiple executing in parallel is possible. But any given execution might be queued behind long-running events.

---

<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:** [July 14, 2020, 9:22pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/11 "2020-07-14T21:22:29Z")

</div>

And you really don’t want to use a lock object, as you’ll block other tasks in the thread pool. If you must do things serialized, start an asynchronous task from your tag event, and have that asynchronous task protect its critical section with a lock object.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [December 8, 2020, 2:05pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/12 "2020-12-08T14:05:22Z")

</div>

Will the following fail me? After reading this thread i think that it is possible a write may occur before the read is finished.

```
temp0 = system.tag.read('[client]ANIMATION/temp0').value
system.tag.writeBlocking(['[client]ANIMATION/temp1'], [temp0])
```

---

<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:** [December 8, 2020, 2:06pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/13 "2020-12-08T14:06:08Z")

</div>

> [@bmeyers](#):
>
> Will the following fail me?

Yes.

Edit: Let me elaborate. `temp1` will get the value you retrieved from `temp0`. Other threads could be writing to either tag simultaneously and disrupt the logic you think will occur.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [December 8, 2020, 2:17pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/14 "2020-12-08T14:17:03Z")

</div>

I understand someone could write to those tags but in terms of ‘continuity’ am I guaranteed to get temp0 back before the second line occurs? If so then what is the point of the function readBlocking/writeBlocking?

Also see this other example:

```
firstName = system.tag.read('[client]HMI/STRING/firstName').value
lastName = system.tag.read('[client]HMI/STRING/lastName').value
operator = str(lastName) + ", " + str(firstName)

```

Surely statements 1 and 2 have to finish before statement 3 starts??? Or is there a difference between client tags and PLC tags for example? Maybe I read the operator name from the PLC in the future.

edit: @pturmel

---

<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:** [December 8, 2020, 2:47pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/15 "2020-12-08T14:47:10Z")

</div>

> [@bmeyers](#):
>
> Surely statements 1 and 2 have to finish before statement 3 starts???

Yes, this is true. Because it is a single script and all calls are blocking. (@maja had a different scenario.)

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [December 8, 2020, 2:49pm UTC](https://forum.inductiveautomation.com/t/tags-writing-and-reading-in-a-script/34611/16 "2020-12-08T14:49:34Z")

</div>

Got it thanks!
