[Tutor] Best way to use excepthook here?

Alex Hall ahall at autodist.com
Fri Jun 24 08:23:17 EDT 2016


Thanks for the responses. It's great to know that nothing funny will happen
even though each job is pulling from utils.py and a few other files. Since
you don't need a fresh copy of every package for every script, I hoped my
own packages would act the same way. I just wasn't sure if excepthook was
some kind of central thing that would act differently, and where this is
mission-critical to where I work, I didn't want to take chances. Oh, and
yes, I did mean sys.excepthook, and I use that in my code. I'd just been
googling "excepthook", so had that in my mind when I emailed.

One related question. This more of a style choice, I know, but I'd like
anyone's thoughts all the same. Currently, in my ADLogger.logExceptions
function, I log the exception (obviously). But I also take the opportunity
to generate and send an email, warning that the job raised an exception and
attaching the log file as well as printing the traceback in the email.
Loggers generally just log, and it feels kind of hacky to include email
code in a function that primarily logs exceptions. At the same time, this
is a perfect place to do it, because it gets called if an exception is
raised, and if that happens, the job almost certainly failed so I need to
know about it. I don't know of another way to do this since I don't have a
Python-based central manager, so can't examine exit codes or anything else.

As to wrapping everything in a try/except, I considered that as well, but
this seems simpler and comes to the same thing. It's one less indentation
level to worry about, too (Notepad doesn't auto-indent, and accessible
editors that do don't always get it right). Plus, nesting try blocks could
get confusing, and in some of these jobs I need tries to transform, say,
non-existent dictionary values into 0 or some other constant.

On Fri, Jun 24, 2016 at 12:28 AM, Peter Otten <__peter__ at web.de> wrote:

> 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.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Alex Hall
Automatic Distributors, IT department
ahall at autodist.com


More information about the Tutor mailing list