[mod_python] Exceptions in the mod_python handler?

Webb Sprague webb.sprague at gmail.com
Sun Jan 6 23:50:20 EST 2008


Hi all,

I have a mod_python handler, called "lc.py", which has a bunch of
functions -- mapped to pages --  called things "index",
"process_request", etc.  Before defining those functions, I connect to
the database with psycopg2 and pass that connection handle to each
function/ page.

<Directory /var/www/localhost/htdocs/lcfit>
    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On
</Directory>

# This is lc.py in the lcfit directory
dbcon = pyscopg2.connect("blah")
def index(req, dbcon=dbcon):
      curs = dbcon.cursor()
      # etc

This works fine usually.  However,  when the connection fails I don't
know how to get apache to send some text to the user saying "call the
admin and tell him to restart postgres."  At the moment I get a nice
stack trace that I know how to read, but I would like to catch the
exception, print something less intimidating, and quit processing the
handler. If I can somehow mark it so that apache knows to reload lc.py
and attempt to reconnect the database the next time (hopefully after
postgres is restarted), that would be especially great (touching the
file?).

Is this a bad design?  I am probably misunderstanding the order in
which things are called (handlers, functions, modules, etc...), but I
don't want each page to connect() and close() on each call, and I
don't know where else to put the connection code except in the main
handler.

Thanks again!


More information about the Mod_python mailing list