Skip to content

Instantly share code, notes, and snippets.

@zeeshanlakhani
Created October 19, 2011 02:27
Show Gist options
  • Save zeeshanlakhani/1297342 to your computer and use it in GitHub Desktop.
Save zeeshanlakhani/1297342 to your computer and use it in GitHub Desktop.
Coroutine Simple Example Python
# grep.py
#
# A very simple coroutine
def grep(pattern):
print "Looking for %s" % pattern
while True:
line = (yield)
if pattern in line:
print line,
# Example use
if __name__ == '__main__':
g = grep("python")
g.next()
g.send("Yeah, but no, but yeah, but no")
g.send("A series of tubes")
g.send("python generators rock!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment