[Stackless-checkins] CVS: slpdev/src/2.2/src/Stackless/demo counter.py, NONE, 1.1

Christian Tismer tismer at centera.de
Thu May 6 22:07:13 CEST 2004


Update of /home/cvs/slpdev/src/2.2/src/Stackless/demo
In directory centera.de:/tmp/cvs-serv27705/2.2/src/Stackless/demo

Added Files:
	counter.py 
Log Message:
added a funny little example on how to sort numbers
which tasklets :-)

--- NEW FILE: counter.py ---
# a little example of tasklets and channels.
# don't use this in production code, it is really nonsense :-)

# We want to sort some random numbers using tasklets.
# The idea is to create a bulk of tasklets which call schedule()
# a number of times. When the count is at zero, the tasklet
# send its result over a channel.
# Guess the result!

import random
from stackless import *

numbers = range(20)
random.shuffle(numbers)
print numbers
# [16, 13, 12, 5, 6, 4, 7, 1, 9, 17, 15, 14, 10, 8, 0, 3, 11, 18, 2, 19]

def counter(n, ch):
	for i in xrange(n):
		schedule()
	ch.send(n)
	
ch=stackless.channel()
for each in numbers:
	stackless.tasklet(counter)(each, ch)

stackless.run()
# now we should have a sorted chain of results in ch
while ch.queue:
	print ch.receive()


_______________________________________________
Stackless-checkins mailing list
Stackless-checkins at stackless.com
http://www.stackless.com/mailman/listinfo/stackless-checkins



More information about the Stackless-checkins mailing list