[Stackless] Video on channels

Arnar Birgisson arnarbi at gmail.com
Sun Sep 9 21:43:50 CEST 2007


Hi all,

On 9/9/07, Hallgrimur H. Gunnarsson <hhg at hhg.to> wrote:
> I watched the same techtalk a while ago and after it I also translated the
> prime sieve example into stackless python as an exercise. The outcome
> was a little different from your version, see below (or http://hhg.to/sieve.py).

I did this as an exercise too, but not with channels. Here's my
version which uses only 2.4 style generators, no stackless needed
(__main__ part stolen from Hallgrímur):

#!/usr/bin/env python

def counter(i=1):
    while True:
        yield i
        i += 1

def filter(p, r):
    for x in r:
        if x % p:
            yield x

def sieve():
    c = counter(2)
    while True:
        p = c.next()
        yield p
        c = filter(p, c)

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 2:
        goal = 100
    else:
        goal = int(sys.argv[1])

    s = sieve()
    for i in xrange(goal):
        print s.next()



-- Arnar




More information about the Stackless mailing list