[pypy-svn] r24189 - pypy/dist/demo

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Mar 9 18:40:54 CET 2006


Author: cfbolz
Date: Thu Mar  9 18:40:53 2006
New Revision: 24189

Added:
   pypy/dist/demo/producerconsumer.py
   pypy/dist/demo/uthread.py
Log:
(pedronis, cfbolz):

example code that uses the capabilities of the logic object space. they indeed
show that using it is not trivial and it is easy to introduce blocked threads
for obscure reasons.



Added: pypy/dist/demo/producerconsumer.py
==============================================================================
--- (empty file)
+++ pypy/dist/demo/producerconsumer.py	Thu Mar  9 18:40:53 2006
@@ -0,0 +1,31 @@
+"""This is an example that uses the (prototype) Logic Object Space. To run,
+you have to set USE_GREENLETS in pypy.objspace.logic to True and do:
+  
+    $ py.py -o logic producerconsumer.py
+
+newvar creates a new unbound logical variable. If you try to access an unbound
+variable, the current uthread is blocked, until the variable is bound.
+"""
+
+def generate(n, limit):
+    print "generate", n, limit
+    if n < limit:
+        return (n, generate(n + 1, limit))
+    return (None, None)
+
+def sum(L, a):
+    print "sum", a
+    head, Tail = newvar(), newvar()
+    L == (head, Tail)
+    if head != None:
+        return sum(Tail, head + a)
+    return a
+
+print "before"
+X = newvar()
+S = newvar()
+S == uthread(sum, X, 0)
+X == uthread(generate, 0, 10)
+print "after"
+
+print S

Added: pypy/dist/demo/uthread.py
==============================================================================
--- (empty file)
+++ pypy/dist/demo/uthread.py	Thu Mar  9 18:40:53 2006
@@ -0,0 +1,29 @@
+"""This is an example that uses the (prototype) Logic Object Space. To run,
+you have to set USE_GREENLETS in pypy.objspace.logic to True and do:
+  
+    $ py.py -o logic uthread.py
+
+newvar creates a new unbound logical variable. If you try to access an unbound
+variable, the current uthread is blocked, until the variable is bound.
+"""
+
+X = newvar()
+Y = newvar()
+
+Y == X
+
+def f():
+    print "starting"
+    print is_unbound(X)
+    if Y:
+        print "ok"
+        return
+    print "false"
+    return
+
+def bind():
+    X == 1
+
+uthread(f)
+print "afterwards"
+uthread(bind)



More information about the Pypy-commit mailing list