git » pymisc » commit 5acfdbb

Add a very simple dataflow object implementation.

author Alberto Bertogli
2005-08-12 19:16:06 UTC
committer Alberto Bertogli
2005-08-12 19:16:06 UTC
parent 7a80bda74a9ad0d814dfee2aea49183cf1fe8b1a

Add a very simple dataflow object implementation.

dataflow.py +38 -0
samples/dataflow/sample1.py +25 -0

diff --git a/dataflow.py b/dataflow.py
new file mode 100644
index 0000000..eeffabf
--- /dev/null
+++ b/dataflow.py
@@ -0,0 +1,38 @@
+
+"""
+dataflow - Objects with dataflow semantics
+Alberto Bertogli (albertogli@telpin.com.ar)
+"""
+
+import threading
+
+class DFObject(object):
+	"Object with dataflow semantics"
+	def __init__(self):
+		self.__isset = False
+		self.__val = None
+		self.__lock = threading.Condition()
+
+	def getval(self):
+		self.__lock.acquire()
+		while not self.__isset:
+			self.__lock.wait()
+		self.__lock.release()
+		return self.__value
+
+	def setval(self, value):
+		self.__lock.acquire()
+		self.__value = value
+		self.__isset = True
+		self.__lock.notifyAll()
+		self.__lock.release()
+
+	val = property(getval, setval)
+
+	def unset(self):
+		self.__lock.acquire()
+		self.__value = None
+		self.__isset = False
+		self.__lock.release()
+
+
diff --git a/samples/dataflow/sample1.py b/samples/dataflow/sample1.py
new file mode 100644
index 0000000..b9e2b95
--- /dev/null
+++ b/samples/dataflow/sample1.py
@@ -0,0 +1,25 @@
+
+import dataflow
+import adatasks
+import time
+
+v = dataflow.DFObject()
+
+@adatasks.task
+def show():
+	print 'show() init + print'
+	print 'show() v value: ' + str(v.val)
+	print 'show() done'
+
+@adatasks.task
+def set():
+	print 'set() init + sleep'
+	time.sleep(2)
+	print 'set() set v to 3'
+	v.val = 3
+	print 'set() done'
+
+
+adatasks.launch()
+adatasks.wait_for_all()
+