Skip to content
Browse files

asynchronous error handling

  • Loading branch information...
1 parent 07ed395 commit 37a064aac13eb993a169edfe24b7e409b6d94a24 @mjrusso committed Jan 7, 2011
Showing with 18 additions and 0 deletions.
  1. +18 −0 pyhbase/async.py
View
18 pyhbase/async.py
@@ -19,15 +19,24 @@
try:
from tornado import httpclient
+ from tornado.stack_context import ExceptionStackContext
except ImportError:
# async operations not supported unless Tornado is installed
httpclient = None
+ ExceptionStackContext = None
class TornadoRequestor(ipc.Requestor):
# TODO: Avro 1.4+: extend `ipc.BaseRequestor` instead of `ipc.Requestor`
def request(self, message_name, request_datum, callback):
+ exception_handler = partial(self._handle_exception, message_name,
@Rustem
Rustem added a note Sep 26, 2013

Just wrap your callback into stack_context.wrap fn and whole context will be restored back. Nicely optimized by @mrjoes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ request_datum, callback)
+
+ with ExceptionStackContext(exception_handler):
+ return self._request(message_name, request_datum, callback)
+
+ def _request(self, message_name, request_datum, callback):
buffer_writer = StringIO()
buffer_encoder = io.BinaryEncoder(buffer_writer)
self.write_handshake_request(buffer_encoder)
@@ -55,6 +64,15 @@ def _on_issue_request(self, message_name, request_datum, callback,
callback(response)
+ def _handle_exception(self, message_name, request_datum, callback, type,
+ value, traceback):
+ """exception handler, called in the event of any uncaught exception"""
+ logging.warning(
+ "caught exception performing avro '{request}' with arguments: "
+ "{args!r}".format(request=message_name, args=request_datum))
+ callback(None) # treat all failed requests as having null responses
+ return True # do not propogate exception to other exception handlers
+
class TornadoHTTPTransceiver(object):

0 comments on commit 37a064a

Please sign in to comment.
Something went wrong with that request. Please try again.