Jorey Bump
list at joreybump.com
Sat Nov 27 18:15:04 EST 2004
Trevor West wrote: > When I was asking about my database problems. Someone mentioned to me > that they kept a single global database connection and just pulled > cursers off that. And while I know how to do that in the context of a > thick client add (we build a big app with wxPython), I'm not sure how to > keep globals in apache other than using the session. I've seen lots of > examples on the web but none of it seems to make much sense... I don't quite have a grip on creating db connection pools, but I've discovered a nice side effect of a solution I posted about earlier about hiding db connection passwords. In order to keep passwords out of the DocumentRoot, I've created a python directory in my home directory and added it to the VirtualHost's PythonPath. This allows me to simply import modules of variables that serve as configuration settings. I've discovered that if I also set up my db connection in this module, it gets reused quite a lot before it is destroyed and replaced by a new one. For example, here is ~/python/settings.py: """ Settings for applications that use the menagerie database. """ import MySQLdb dbhost = "localhost" dbuser = "cyril" dbpwd = "ladidi123" dbname = "menagerie" dbh = MySQLdb.connect(dbhost, dbuser, dbpwd, dbname) Then in my application, I import all of the values: from settings import * Now I can use the same dbh for the life of the interpreter, rather than creating a new one within the script. IOW, each apache process seems to get a db handle, instead of each request. The handle is also shared by any other scripts that import the settings module within a single interpreter. This is on Debian woody: Apache/1.3.26 (Unix) Debian GNU/Linux mod_python/2.7.8 Python/2.1.3
|