[Tutor] Best way to use excepthook here?
Peter Otten
__peter__ at web.de
Fri Jun 24 00:28:41 EDT 2016
Alex Hall wrote:
> Hey all,
> How would I go about sharing a reassignment of excepthook, where that
> assignment is made to a central function and could come from any of
> several files?
>
> I'm replacing a bunch of SQL jobs at work, and my boss said he doesn't
> care which language I use. Naturally, Python it is. :)
>
> All these jobs do different things, and run at different times and
> different intervals. Some update files on our FTP server, some email the
> previous day's shipping errors, some back up databases, and so on. The
> setup I'm going to try first is one file per job, with utils.py and a
> couple others shared between them all. That way, any job that needs to
> email can just
> import emailer
> and every job can access configuration data:
> import utils
> utils.config["emails"]["alexHall"]
>
> One thing that utils.py provides is logging:
>
> import utils
> logger = utils.ADLogger("Job Name")
>
> The ADLogger object sets the level, the file name, the formatter, and so
> on. It also has a function: logException(self, type, value, traceback).
> This simply logs the exception, since ADLogger is a subclass of
> logging.Logger.
>
> Finally, my question. Say job1 and job2 run within a minute of each other,
> but job1 takes a while to execute. Both jobs set their excepthook
> functions:
>
> logger = utils.ADLogger("job1")
> excepthook = logger.logException
>
> logger = ADLogger("Job 2")
> excepthook = logger.logExceptions
Did you mean
sys.excepthook = logger.logExceptions
?
> Now, Python is running two scripts which each have their excepthook set to
> something different. What happens when both try to log an exception, or
> even if one tries to while the other is running? Will this work how I
> hope, or will there be confusion? Should I thread it somehow? I'd rather
> not have a central manager, as that's one more thing that might fail; I
> plan to set all the scripts up in Windows Task Scheduler. I can have a
> Python-based manager if I need to, but it'd take me a while to finish that
> (I started something similar months back and would have to finish and
> modify it). Am I worrying over nothing, or will I need to do something to
> make sure each job logs exceptions, and does it correctly, no matter who
> else is running at the same time? Thanks.
Even though setting it to different functions in different scripts should
work as expected I'd rather not alter sys.excepthook.
How about
try:
job1() # or main() or whatever
except:
logger.exception("Job 1 failed with")
? This doesn't rely on setting a central global variable.
More information about the Tutor
mailing list