diff options
author | Ben Bangert <ben@groovie.org> | 2012-08-07 11:18:28 -0700 |
---|---|---|
committer | Ben Bangert <ben@groovie.org> | 2012-08-07 11:18:28 -0700 |
commit | 3bd7c2b9cfd95f5d5c374999794f75516db395b3 (patch) | |
tree | 020a4ac1635bf44bb0a2408cf560dca51c0a913d /kazoo/recipe/watchers.py | |
parent | 418481d3738a2cfdb83550b37eccd8236a1a3de8 (diff) | |
download | kazoo-3bd7c2b9cfd95f5d5c374999794f75516db395b3.tar.gz |
Spawn _get_data from session listener to avoid blocking session events.
Set data to None, None if the child doesn't exist, or is deleted later.
Fixes #14.
Diffstat (limited to 'kazoo/recipe/watchers.py')
-rw-r--r-- | kazoo/recipe/watchers.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/kazoo/recipe/watchers.py b/kazoo/recipe/watchers.py index 08e8e6e..481ffa6 100644 --- a/kazoo/recipe/watchers.py +++ b/kazoo/recipe/watchers.py @@ -5,6 +5,7 @@ import time from functools import partial from kazoo.client import KazooState +from kazoo.exceptions import NoNodeException log = logging.getLogger(__name__) @@ -30,6 +31,14 @@ class DataWatch(object): # Above function is called immediately and prints + Error Handling + -------------- + + In the event the node does not exist, the function will be called + with ``(None, None)`` and will not be called again. This should be + considered the last function call. This behavior will also occur + if the node is deleted. + """ def __init__(self, client, path, func=None, allow_session_lost=True): @@ -91,8 +100,14 @@ class DataWatch(object): if self._stopped: return - data, stat = self._client.retry(self._client.get, - self._path, self._watcher) + try: + data, stat = self._client.retry(self._client.get, + self._path, self._watcher) + except NoNodeException: + self._stopped = True + self._func(None, None) + return + if not self._watch_established: self._watch_established = True @@ -120,7 +135,7 @@ class DataWatch(object): self._watch_established = False elif state == KazooState.CONNECTED and \ not self._watch_established and not self._stopped: - self._get_data() + self._client.handler.spawn(self._get_data) class ChildrenWatch(object): |