[mod_python] Any support for on-time Initialization?

Michael C. Neel neel at mediapulse.com
Mon Feb 2 14:33:55 EST 2004

>From my own adventures, MySQL is okay with having a large number of
connections open - it doesn't seem to affect performance.  Just make
sure your connection limit is set at or higher than your apache process
limit; there will be a one-to-one tie here assuming each apache child
makes only one connection.

Also, MySQL seems to be okay with not getting an explicit db.close()
call, so you can connect to the database either in the first request and
reuse that from there on out, or connect when the module is imported,
like such:

from modpython import apache
import MySQLdb

db = MySQLdb.connect()

def handler(req):
	c = db.cursor()
	## do stuff
	return apache.OK

Which can be the easiest if your database settings can be hard coded.
Like Daniel said, going with a threaded MPM is not for the faint of
heart, as MySQLdb doesn't like sharing connections very much.

If your not using MySQL you may be able to get away with the above, but
do some testing first to be safe.

> -----Original Message-----
> From: Daniel West [mailto:dwmp at opti.cgi.net] 
> Sent: Monday, February 02, 2004 1:30 PM
> To: mod_python at modpython.org
> Subject: Re: [mod_python] Any support for on-time Initialization?
> Jalil,
> The best way to setup database connections is just to wait 
> for the first 
> request to come in.  Keep your database connection in a 
> module name space 
> that all of your pages share and it'll persist between 
> connections so long 
> as you don't explicitly close it.  Once you import a module 
> it stays in the 
> interpreter name space between requests so future requests 
> have access to 
> the module without having to load it again.
> If you're interested in keeping "pools" of database 
> connections then you 
> may be considering one of the multi-threaded MPM's for Apache 
> 2.  You'll 
> save yourself a lot of hair pulling if you stick with the 
> prefork mpm.  It 
> bears saying that in the prefork mpm, each process does not share 
> interpreter resources with the other processes.  This means 
> each process 
> will have it's own "pool" of database connections which may 
> not bode well 
> with your database server if you run a moderately heavy Apache load.
> -Dan
> At 10:12 AM 2/2/2004 -0800, you wrote:
> >I was wondering if there is any support for one-time 
> initialization for 
> >applications in mod-python - similar to the init() call of a 
> servlet for 
> >example. I would like to set up a database connection pool 
> but I need to 
> >do it only once when my app starts.
> >
> >If there is no support for initialization, what is the best 
> way to set up 
> >things that need to be set up only once per app in mod-python?
> >
> >Thanks,
> >
> >-Jalil
> >
> >_______________________________________________
> >Mod_python mailing list
> >Mod_python at modpython.org
> >http://mailman.modpython.org/mailman/listinfo/mod_python
> _______________________________________________
> 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