Created
July 11, 2015 04:05
-
-
Save sigmavirus24/318102d7db3cdd77e06e to your computer and use it in GitHub Desktop.
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
class Iter(object): | |
def __init__(self, iterable): | |
self.iterable = iterable | |
self.closed = False | |
def __iter__(self): | |
try: | |
for chunk in self.iterable: | |
yield chunk | |
finally: | |
self.closed = True | |
iter = Iter(range(5)) | |
try: | |
for i in iter: | |
raise IOError() | |
except IOError: | |
pass | |
assert iter.closed is True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rather than calling
gc.collect
which does not really guarantee that all garbage will be collected, you want to restructure things so that you manage resources in your main routine rather than the generator coroutine:Otherwise you need to ensure that you explicitly close all generators: