Source

green380 / examples / prime_sieve.py

Full commit
#!/usr/bin/env python3.3

from green380 import *
import sys

def generate(ch, n):
    for i in range(2, n+1):
        yield from ch.put(i)

def filter(p, in_, out):
    while 1:
        n = yield from in_.get()
        if n % p:
            yield from out.put(n)

def sieve(n):
    ch = Channel()
    spawn(generate, ch, n)
    while True:
        p = yield from ch.get()
        print(p)
        ch1 = Channel()
        spawn(filter, p, ch, ch1)
        ch = ch1

spawn(sieve, int(sys.argv[1]) if len(sys.argv) > 1 else 10000)
run()