How To Create A Custom Generator Class That Is Correctly Garbage Collected
Solution 1:
PEP342, states:
[generator].__del__()
is a wrapper for[generator].close()
. This will be called when the generator object is garbage-collected ...
The Generator class in collections.abc does not implement __del__
, and neither do its superclasses or metaclass.
Adding this implementation of __del__
to the class in the question results in the lock being freed:
class CustomGeneratorClass(Generator):
...
def __del__(self):
self.close()
Output:
Generator Class Initialised: I grabbed a lock
Recieved 0
Recieved 1
Recieved 2
Recieved 3
Exception Thrown in Generator: I let go of the lock
Finished: Lock was free
Caveat:
I'm not experienced with the intricacies of object finalisation in Python, so this suggestion should be examined critically, and tested to destruction. In particular, the warnings about __del__
in the language reference should be considered.
A higher-level solution would be to run the generator in a context manager
with contextlib.closing(CustomGeneratorClass(100, lock)):
# do stuff
but this is cumbersome, and relies on users of the code remembering to do it.
Post a Comment for "How To Create A Custom Generator Class That Is Correctly Garbage Collected"