[mod_python] modpython, mysqldb best paractice

Martijn Moeling martijn at xs4us.nu
Wed Jul 19 05:41:55 EDT 2006

Hi all,


I started the development of a specific kind of CMS over 2 years ago,
and due to sparse documentation I am still puzzled about a few things.

Basically it consist of one .py and a mysqldatabase with all the data
and templates, ALL pages are generated on the fly.


First of all I am confused about PythonInterPerDirectory and

In the way I use modpython.


My apache has no configured virtual hosts since my CMS can handle this
on its own by looking at req.host

On one site which is running on my system ( http://www.mkbok.nl
<http://www.mkbok.nl/>  ) we use different subdomains, so basically 

the page to be build is derived from the req.host (e.g.
xxx.yyy.mkbok.nl) where xxx and yyy are variable. 

This is done by DNS records like   *     A      ip1.ip2.ip3.ip4


My next problem seems mysql and mysqldb.

Since I do not know which website is requested (multiple are running on
that server) I open a database connection, do my stuff and close the

Again the database selection comes from req.host, and here the
domainname is used for database selection.


The system runs extremely well, but once in a while the webserver
becomes so busy that it does not respond to page request anymore. 

We suspect that mysql is the problem here since the only thing we can
see is mysql is consuming more and more swapspace and at some point it
runs out of resources and starts looping. At that point the system
(Linux) keeps running but with a 100% cpu utilization and we are unable
to login and investigate. 

So logging in to the UPS remotely and power down the system by virtually
unplugging the cable is the only (and BAD) solution.


So what is best practice when you have to connect to mysql with mysqldb
in a mod_python environment keeping in mind that the database connection
has to be build every time a visitor requests a page? Think in terms of
a "globally" available db or db.cursor connection.

Since global variables are troublesome in the .py contaning the handler
I use a class from which an instance is created every time a client
connects and the DB connection is global to that class, ist that wrong?


What happenens if mod_python finds an error before my mysqldb connection
is closed (not that this happenes a lot, but it does happen, sorry)


Also I do not understand the req.register_cleanup() method, what is
cleaned up and what not?




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060719/40cc3f3f/attachment-0001.html

More information about the Mod_python mailing list