Permalink
Browse files

Example updates.

  • Loading branch information...
1 parent 8883f97 commit 90f0b256a1fba9ea596d8b124a2e69d802f4a5e6 Jamie Turner committed Sep 17, 2010
Showing with 34 additions and 24 deletions.
  1. +21 −0 examples/event.py
  2. +13 −23 examples/queue.py
  3. +0 −1 examples/synchronized.py
View
@@ -0,0 +1,21 @@
+from diesel.util.event import Event
+from diesel import quickstart, quickstop, sleep
+
+pistol = Event()
+
+def racer():
+ pistol.wait()
+ print "CHAAARGE!"
+
+def starter():
+ print "Ready..."
+ sleep(1)
+ print "Set..."
+ sleep(1)
+ print " ~~~~~~~~~ BANG ~~~~~~~~~ "
+ pistol.set()
+ sleep(1)
+ print " ~~~~~~~~~ RACE OVER ~~~~~~~~~ "
+ quickstop()
+
+quickstart(starter, [racer for x in xrange(8)])
View
@@ -1,45 +1,35 @@
from diesel.util.queue import Queue, QueueTimeout
-from diesel import log as glog, Application, Loop, sleep
+from diesel.util.event import Countdown
+from diesel import log as glog, sleep, quickstart, quickstop
q = Queue()
+cd = Countdown(4)
def putter():
log = glog.sublog("putter", glog.info)
- log.info("putting 50000 things on log")
- for x in xrange(50000):
- q.put(x)
- sleep()
-
- log.info("done, sleeping for 10s...")
-
- sleep(10)
-
- log.info("putting 50000 *more* things on log")
- for x in xrange(50000, 100000):
+ log.info("putting 100000 things on log")
+ for x in xrange(100000):
q.put(x)
sleep()
def getter():
log = glog.sublog("getter", glog.info)
got = 0
- while got < 100000:
+ while got < 25000:
try:
s = q.get(timeout=3)
+ sleep()
except QueueTimeout:
log.warn("timeout before getting a value, retrying...")
continue
- assert s == got
got += 1
- if got % 10000 == 0:
- log.info("up to %s received, sleeping for 0.5s" % got)
- sleep(0.5)
+ log.info("SUCCESS! got all 25,000")
+ cd.tick()
- log.info("SUCCESS! got all 100,000")
- a.halt()
+def manage():
+ cd.wait()
+ quickstop()
-a = Application()
-a.add_loop(Loop(putter))
-a.add_loop(Loop(getter))
-a.run()
+quickstart(manage, putter, [getter for x in xrange(4)])
View
@@ -13,7 +13,6 @@ def free_loop():
def sync_loop():
global sync
- id = random.random()
with synchronized():
sync += 1
sleep(random.random())

0 comments on commit 90f0b25

Please sign in to comment.