Created
September 6, 2016 09:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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