diff options
author | Dana Powers <dana.powers@gmail.com> | 2016-06-18 14:51:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-18 14:51:23 -0700 |
commit | 6271c02c6eebf52a6d368416db49bfa57b09ef04 (patch) | |
tree | 795b70a6c0eb144cd9da6c09f29ff866236734a9 /kafka/util.py | |
parent | 5b9c55817b76eab8346f65e7c973c518d1e82409 (diff) | |
download | kafka-python-6271c02c6eebf52a6d368416db49bfa57b09ef04.tar.gz |
Use weakref when registering a producer.close atexit to fix normal gc (#728)
* Use weakref when registering a producer.close atexit to fix normal gc
* Test that del(producer) terminates async thread
Diffstat (limited to 'kafka/util.py')
-rw-r--r-- | kafka/util.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/kafka/util.py b/kafka/util.py index 18c39a4..b3a72f3 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -1,3 +1,4 @@ +import atexit import binascii import collections import struct @@ -188,3 +189,12 @@ class WeakMethod(object): if not isinstance(other, WeakMethod): return False return self._target_id == other._target_id and self._method_id == other._method_id + + +def try_method_on_system_exit(obj, method, *args, **kwargs): + def wrapper(_obj, _meth, *args, **kwargs): + try: + getattr(_obj, _meth)(*args, **kwargs) + except (ReferenceError, AttributeError): + pass + atexit.register(wrapper, weakref.proxy(obj), method, *args, **kwargs) |