# Tag value changed event synchronization

**URL:** <https://forum.inductiveautomation.com/t/tag-value-changed-event-synchronization/27653>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [August 22, 2019, 9:42am UTC](https://forum.inductiveautomation.com/t/tag-value-changed-event-synchronization/27653 "2019-08-22T09:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgmo](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@pgmo](https://forum.inductiveautomation.com/u/pgmo)\
**Post date:** [August 22, 2019, 9:42am UTC](https://forum.inductiveautomation.com/t/tag-value-changed-event-synchronization/27653/1 "2019-08-22T09:42:42Z")

</div>

Some of my tags have their `valueChanged` event that executes a common piece of code: say that tags **Tets1** and **Test2** execute the same `longTask` routine when their values change. The `longTask` routine reads a tag value, say tag **target** , increments that value and writes the value back to the same tag:

```auto
def longTask(tagPath):
	val=system.tag.read("target").value
	time.sleep(1)
	system.tag.write("target",val+1)

```

If by chance more then one event occurs at the same time, they must synchronized, so that no one gets the same **target** value as another.  
For that purpose I started off with the python `threading` module like this:

```auto
import time
import threading
lock=threading.Lock()

def longTask(tagPath):
	lock.acquire(True)
	val=system.tag.read("target").value
	time.sleep(1)
	system.tag.write("target",val+1)
	lock.release()

```

The revised code seems not to work at all!  
Then I switched to `java.util.concurrent.locks` package and rewrote the code like this:

```auto
import time
from java.util.concurrent.locks import ReentrantLock
rlock=ReentrantLock()

def longTask(tagPath):
	
	rlock.lock()
	val=system.tag.read("target").value
	time.sleep(1)
	system.tag.write("target",val+1)
	rlock.unlock()

```

That last version seems to work perfectly.  
Therefore my questions:

- what is the best practice to synchronize `valueChanged` events?
- why does python `threading` module seem not to work? Am I doing anything wrong with it?

Thanks in advance, regards

---

<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 22, 2019, 11:25am UTC](https://forum.inductiveautomation.com/t/tag-value-changed-event-synchronization/27653/2 "2019-08-22T11:25:15Z")

</div>

I haven’t looked close, but I suspect it is an artifact of jython’s threads really being able to run concurrently, where python’s threads don’t–at least, not in the interpreter. In general, if both java and python offer a way to do something, use the java way in jython.

---

<div class="post-metadata">

**Author:** ![pgmo](https://avatars.discourse-cdn.com/v4/letter/p/d07c76/32.png) [@pgmo](https://forum.inductiveautomation.com/u/pgmo)\
**Post date:** [August 22, 2019, 12:05pm UTC](https://forum.inductiveautomation.com/t/tag-value-changed-event-synchronization/27653/3 "2019-08-22T12:05:10Z")

</div>

> [@pturmel](#):
>
> use the java way in jython

Ok, thanks pturmel, I will go that way!
