[Stackless] Dining Philosophers with Join Patterns and Stackless.py
Andrew Francis
andrewfr_ice at yahoo.com
Mon Aug 22 18:31:28 CEST 2011
Hi Folks:
As I stated in a previous post, I have written a new version of stackless.py incorporating a concept called join patterns. Join patterns build up on the work I have done with stackless.select(). I am still experimenting with the API amongst other things. So things look rough. When I get stuff stabilized, I'll post a new version of stackless.py in the stackless repository.
With the exception of a hiccup of a recursion limit problem (there is a weird interaction with Twisted), here is the core of the dining philosophers that roughly mimics the example in the paper "Scalable Join Patterns." I left out the Twisted parts.
def thinker(name, thinking, left, right):
global pattern
print "Philosopher %s left=%s right=%s" % (name, left.label, right.label)
utensils = stackless.joinPattern([stackless.JoinReceiveChanop(left.receiveCase()), \
stackless.JoinReceiveChanop(right.receiveCase())])
patterns.append(utensils)
while True:
waitTime = random.randint(thinking[0], thinking[1])
print name, "thinking for ", waitTime, "time units"
tick(waitTime)
print name, "ready to eat"
result = stackless.select([utensils])
for p in result:
print p.value, " relenquished chopstick ", p.channel.label
utensils.reset()
print name, " finished eating"
release(left, name)
release(right, name)
Since I didn't build buffered channels into the new stackless.py, I have to do asynchrony in the following way:
def release(aChannel, name):
def __release():
aChannel.send(name)
stackless.tasklet(__release)()
I would appreciate feedback.
Cheers,
Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20110822/be69f6cd/attachment.html>
More information about the Stackless
mailing list