Skip to content

Commit

Permalink
Example updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Turner committed Sep 17, 2010
1 parent 8883f97 commit 90f0b25
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
21 changes: 21 additions & 0 deletions examples/event.py
@@ -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)])
36 changes: 13 additions & 23 deletions examples/queue.py
@@ -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)])
1 change: 0 additions & 1 deletion examples/synchronized.py
Expand Up @@ -13,7 +13,6 @@ def free_loop():

def sync_loop():
global sync
id = random.random()
with synchronized():
sync += 1
sleep(random.random())
Expand Down

0 comments on commit 90f0b25

Please sign in to comment.