Created
December 15, 2009 15:52
-
-
Save Arbow/257034 to your computer and use it in GitHub Desktop.
stackless python prime generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 相比sieve.go,内存占用少很多哦 - 对比 http://gist.github.com/257036 | |
import stackless | |
def generate(channel): | |
i = 2 | |
while True: | |
channel.send(i) | |
i = i+1 | |
def filter_prime(in_chan, out_chan, prime): | |
while True: | |
num = in_chan.receive() | |
if num % prime != 0: | |
out_chan.send(num) | |
def main(): | |
channel = stackless.channel() | |
stackless.tasklet(generate)(channel) | |
while True: | |
prime = channel.receive() | |
print prime | |
new_chan = stackless.channel() | |
stackless.tasklet(filter_prime)(channel, new_chan, prime) | |
channel = new_chan | |
stackless.tasklet(main)() | |
stackless.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment