Skip to content

Instantly share code, notes, and snippets.

@kaiix
Created November 25, 2014 04:31
Show Gist options
  • Select an option

  • Save kaiix/b9f0e2b32f2380d52458 to your computer and use it in GitHub Desktop.

Select an option

Save kaiix/b9f0e2b32f2380d52458 to your computer and use it in GitHub Desktop.
generator based coroutine
import random
q = []
def producer():
while True:
item = random.randrange(100)
print 'produce', item
yield item
def consumer():
while True:
item = yield
print 'consume', item
p = producer()
c = consumer()
c.send(None)
i = 0
while True:
if i == 10:
break
c.send(p.next())
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment