Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created April 9, 2013 21:15
Show Gist options
  • Save urschrei/5349468 to your computer and use it in GitHub Desktop.
Save urschrei/5349468 to your computer and use it in GitHub Desktop.
A simple coroutine example
def lazy_writer():
with open("out.txt", "w") as o:
while True:
line = (yield)
# do whatever you like to line here
out = "%s%s\n" % (line, " added")
o.write(out)
wl = lazy_writer()
# initialise the coroutine before you send anything in
wl.next()
for line in open("in.txt", "r"):
wl.send(line.rstrip())
wl.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment