[mod_python] the req object

Graham Dumpleton graham.dumpleton at gmail.com
Thu Jun 14 19:23:48 EDT 2007

On 15/06/07, David Bear <David.Bear at asu.edu> wrote:
> On Wed, Jun 13, 2007 at 04:15:38PM -0400, Jim Gallacher wrote:
> > Hi David,
> >
> > David Bear wrote:
> > >When is the req object made available?
> > >
> > >
> > >when the import dummymodule fails, the req object is not available to
> > >it. Without the req object, how does one send output anywhere?
> >
> > That's a strange bit of code you have there. ;) Your code would only get
> > executed by mod_python once, when the module is first loaded.
> strange becuase its only illustrates how I think I want to catch
> exceptions on module imports.
> Thank you so much for the great explanation below. I'll read it a
> third time to see if I can make more sense of it.
> Prior to that, what recommendations are there for catching exceptions
> on module import failures. There is a vast amount of functionality
> that isn't available untill modules are correctly imported. So I see a
> heirarchy of needs -- modules that MUST import or the script MUST die.
> Then, modules that should import or display and error to the browser.
> Then, modules that could important -- for better, faster, stronger
> ways of doing something, or my code could resort to doing something
> different.
> So how would I handle the except when
> 1) critical module import fails and NO access to any apache data is
> available, i.e., failure to import mod_python

If it is actually a syntax error or really unexpected exception then
just let it be raised and don't try and catch it. The actual failure
to import the module will result in a 500 error returned to the HTTP
client which may have triggered the input through the loading of a
specific handler. The details of the exception will also appear in the
error log.

When such errors occurs, at least for a handler module managed by the
mod_python importer it will not cache the broken module and on the
next request will try and import it yet again. If the module which
broke was on sys.path though it will be stuck in memory and an Apache
restart required after fixing it.

If it is some type of runtime error, for example related to creating a
database connection, then catch the exception and log to the Apache
error log using apache.log_error() and some message of your own. Your
code should track when it fails and when a handler subsequently tries
to use what failed to work (connect), should retry the operation again
and if it succeeds keep going otherwise log problem and raise more
specific 500 error page to the HTTP client.

In other words, if you are trying to do something up front at time of
module import and that operation can be retried, log a message but
allow the handler to retry it when it is actually first required as


> 2) operational module files to import and I need to send the message
> to the client browser
> 3) import failure where a nice message is just included in the output
> stream (well, I think I can figure this one out)
> number 1 above seems the most problematic.
> >
> >
> --
> David Bear
> phone:  602-496-0424
> fax:    602-496-0955
> College of Public Programs/ASU
> University Center Rm 622
> 411 N Central
> Phoenix, AZ 85007-0685
>  "Beware the IP portfolio, everyone will be suspect of trespassing"
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list