Tags writing and reading in a script

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?

Yes if you have a script like the following:

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

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.

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

2 Likes

.

1 Like

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.

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

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?

No. You have race that depends on random timing.

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?

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.

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.

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])

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.

1 Like

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

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

Got it thanks!