Skip to content

Instantly share code, notes, and snippets.

@schmir
Created April 15, 2013 21:51
Show Gist options
  • Save schmir/5391563 to your computer and use it in GitHub Desktop.
Save schmir/5391563 to your computer and use it in GitHub Desktop.
# allow to silence error reporting for certain 'expected exceptions'
# gevent prints a error report including a traceback for each failed
# greenlet. this is at times useful, but you're currently forced to
# either handle exceptions in those greenlets or live with the traceback
# on stderr.
# patch_gevent introduces a class variable 'expected_exceptions' to
# gevent's greenlet class. This is a tuple of exceptions for which no
# error report should be printed to stderr.
# This can be set in a custom greenlet subclass, or even at runtime like
# in:
# ,----
# | from gevent import spawn, getcurrent
# |
# | def divide():
# | getcurrent().expected_exceptions += (ZeroDivisionError,)
# | 1/0
# |
# | spawn(divide).join()
# `----
from gevent import hub, greenlet
def handle_error(self, context, type, value, tb):
expected_exceptions = getattr(context, "expected_exceptions", self.NOT_ERROR)
if not issubclass(type, expected_exceptions):
self.print_exception(context, type, value, tb)
if context is None or issubclass(type, self.SYSTEM_ERROR):
self.handle_system_error(type, value)
def patch_gevent():
greenlet.Greenlet.expected_exceptions = hub.Hub.NOT_ERROR
hub.Hub.handle_error = handle_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment