[mod_python] Lifespan of objects

Nic Ferrier nferrier at tapsellferrier.co.uk
Thu Aug 19 21:46:13 EDT 2004


Svenne Krap <svenne at krap.dk> writes:

> Hi.
> 
> Firstly, I'm running modpython 3.1.3 on apache2 (2.0.50).
> 
> I have been fooling around with ModPython a little, but I am having 
> trouble to figure out lifetime of objects.
> 
> Let's consider this
> 
> ==== test.py starts ====
> from mod_python import apache
> import MYSQLdb
> _db = None
> 
> def getdb():
>     global _db
>     if _db is None:
>        _db = MYSQLdb.connect(....)
> 
> def handler(req):
>     cnx = getdb()
>     cur = cnx.cursor()
>     ...
>     return apache.OK
> 
> ==== test.py ends====
> 
> As far as I can figure out from the docs, the following should be true:
> 
> 1) _db is initialized exactly once for every apache process and lives as 
> long as the process
> 2) cnx is copied from _db at every request, but freed after handler returns
> 3) cur is created at every request, but freed at handler return
> 
> When I run a slightly more complex script (with the same idea though), I 
> keep getting "too many connections" back from mysql at some point.
> There is only one python file served and no custom modules are loaded, 
> so it should not be because I got too many interprenters started up.
> This is after 100 connections are made, but since I am the only one 
> (guaranteed) to access the script, 100 simultanious connections is utopia.
> 
> I use an extensive amount of subdomains, but everything is run through 
> one wild-carded vhost.
> 
> I usually have around 20 apache2 processes hanging around, but nothing 
> else is using mysql.
> 
> Can anyone please point out, where my problem is? And perhaps provide a 
> pointer to a good way to pool ressources, the tutorial in the docs seems 
> a little meager.

Are you just hitting some MySQL cursor limit? You are using a single
connection in parallel, not a good idea.

I'd say you need to use a connection pool and get and return a
connection from the pool with every request.


Nic


More information about the Mod_python mailing list