Created
April 9, 2013 21:15
-
-
Save urschrei/5349468 to your computer and use it in GitHub Desktop.
A simple coroutine example
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
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