stackless python改prime number一则

修改自http://blog.csdn.net/sedgewick/article/details/6774419基本是直译代码的.

原理我把图拷过来了.

代码总体还是很简单的..

import stackless

def generate(ch):
    for i in range(2,1000):
        ch.send(i)    
        
def filter(in_ch,out_ch,primer):
    while True:
        i = in_ch.receive()
        if i % primer != 0 :
            out_ch.send(i)
    
def main(ch):
    cur_ch = ch
    while True:
        prime = cur_ch.receive()
        print prime
        new_ch = stackless.channel()
        stackless.tasklet(filter)(cur_ch,new_ch,prime)
        cur_ch = new_ch       
    
ch = stackless.channel()  
stackless.tasklet(generate)(ch)
stackless.tasklet(main)(ch)

stackless.run()

 

posted @ 2012-10-23 09:24  zhuangzhuang1988  阅读(222)  评论(0编辑  收藏  举报