Skip to content

Instantly share code, notes, and snippets.

@Arbow
Created December 15, 2009 15:52
Show Gist options
  • Save Arbow/257034 to your computer and use it in GitHub Desktop.
Save Arbow/257034 to your computer and use it in GitHub Desktop.
stackless python prime generator
# 相比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