[Stackless] Video on channels
Grant Olson
olsongt at verizon.net
Sun Sep 9 03:05:37 CEST 2007
I finally got around to watching a techtalk Rob Pike did on the langauage
Newsqueak, which uses channels extensively. It's obviously not stackless
specific, but gives plenty of good motivations for channels as the way of
the future.
http://video.google.com/videoplay?docid=810232012617965344
In particular, there was a really elegant prime number sieve. You really
need to see the slide explaining it to see how brilliant it is. Anyway,
here's the stackless version of the sieve:
from stackless import *
class sieve:
def __init__(self, start_num):
self.ch = channel()
self.start_num = start_num
self.next_sieve = None
print start_num
tasklet(self.listen)()
def listen(self):
while 1:
current_num = self.ch.receive()
if current_num % self.start_num == 0:
pass # not prime
else:
if not self.next_sieve:
self.next_sieve = sieve(current_num)
self.next_sieve.ch.send(current_num)
class counter:
def __init__(self,number,send_to):
self.number = number
self.ch = channel()
self.send_to = send_to
tasklet(self.listen)()
def listen(self):
while 1:
self.ch.receive()
self.send_to.send(self.number)
self.number += 1
def send(self):
self.ch.send(1)
prime_sieve = sieve(2)
current_counter = counter(2,prime_sieve.ch)
for i in range(100):
current_counter.send()
More information about the Stackless
mailing list