Skip to content

Register atexit handlers for consumer and producer thread/multiprocess cleanup #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions kafka/consumer/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import atexit
import logging
import numbers
from threading import Lock
Expand Down Expand Up @@ -75,6 +76,11 @@ def __init__(self, client, group, topic, partitions=None, auto_commit=True,
for partition in partitions:
self.offsets[partition] = 0

# Register a cleanup handler
def cleanup(obj):
obj.stop()
self._cleanup_func = cleanup
atexit.register(cleanup, self)

def fetch_last_known_offsets(self, partitions=None):
if self.group is None:
Expand Down Expand Up @@ -157,14 +163,30 @@ def _auto_commit(self):
if self.count_since_commit >= self.auto_commit_every_n:
self.commit()

def __del__(self):
self.stop()

def stop(self):
if self.commit_timer is not None:
self.commit_timer.stop()
self.commit()

if hasattr(self, '_cleanup_func'):
# Remove cleanup handler now that we've stopped

# py3 supports unregistering
if hasattr(atexit, 'unregister'):
atexit.unregister(self._cleanup_func) # pylint: disable=no-member

# py2 requires removing from private attribute...
else:

# ValueError on list.remove() if the exithandler no longer
# exists is fine here
try:
atexit._exithandlers.remove((self._cleanup_func, (self,), {}))
except ValueError:
pass

del self._cleanup_func

def pending(self, partitions=None):
"""
Gets the pending message count
Expand Down
27 changes: 26 additions & 1 deletion kafka/producer/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import atexit
import logging
import time

Expand Down Expand Up @@ -152,7 +153,11 @@ def __init__(self, client, async=False,
self.thread.daemon = True
self.thread.start()


def cleanup(obj):
if obj.stopped:
obj.stop()
self._cleanup_func = cleanup
atexit.register(cleanup, self)

def send_messages(self, topic, partition, *msg):
"""
Expand Down Expand Up @@ -213,6 +218,26 @@ def stop(self, timeout=1):

if self.thread.is_alive():
self.thread_stop_event.set()

if hasattr(self, '_cleanup_func'):
# Remove cleanup handler now that we've stopped

# py3 supports unregistering
if hasattr(atexit, 'unregister'):
atexit.unregister(self._cleanup_func) # pylint: disable=no-member

# py2 requires removing from private attribute...
else:

# ValueError on list.remove() if the exithandler no longer exists
# but that is fine here
try:
atexit._exithandlers.remove((self._cleanup_func, (self,), {}))
except ValueError:
pass

del self._cleanup_func

self.stopped = True

def __del__(self):
Expand Down