Skip to content

Instantly share code, notes, and snippets.

@alexprengere
Created September 6, 2016 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexprengere/cf160c8161f72f4e51e009e0fa516ed6 to your computer and use it in GitHub Desktop.
Save alexprengere/cf160c8161f72f4e51e009e0fa516ed6 to your computer and use it in GitHub Desktop.
A decorator to force the logging of uncatched exceptions, useful for workers with multiprocessing
import traceback
from functools import wraps
def log_exceptions(fd):
def _wrapped(func):
@wraps(func)
def _func(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
fd.write(traceback.format_exc())
fd.flush()
raise
return _func
return _wrapped
import sys
@log_exceptions(sys.stderr)
def test():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment