[mod_python] Apache on Windows and concurrency

Nicolas Lehuen nicolas at lehuen.com
Wed Jul 7 12:12:45 EDT 2004

Hi Ian,

You can also use the standard "thread" or "threading" Python modules, so
that you don't depend on _apache.

The approach you have is secure but it has the downside of denying any
concurrent access to the database (if I understand your code correctly). I
don't know SQLite very much, but it seems that it can handle multithreaded
access to the database (see  <http://www.sqlite.org/lockingv3.html>
http://www.sqlite.org/lockingv3.html), so it would be better to leverage
this. The solution is a connection pool.

You can write a connection pool using the threading.RLock and
threading.Condition to protect the critical sections, namely the acquisition
of database connections. The end result is that each thread is guaranteed to
have a connection of its own, which can be reused as soon the thread release
it to the pool. It usually speeds things up, and I guess it would be
especially useful with SQLite. I've been using such a connection pool for a
few months now and have never looked back since. In your case, it seems that
you wrap the SQLite connection with an UserDatabase object, so maybe you
should pool UserDatabase instances instead of SQLite connections.

Best regards,

Nicolas Lehuen


De : mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] De la part de Iain Mackay
Envoyé : mardi 6 juillet 2004 21:18
À : mod_python at modpython.org
Objet : [mod_python] Apache on Windows and concurrency

Thanks Grisha for being so quickly on the case.

Session.py also includes the solution to my problem, using the Apache global

I now have the following code to protect a section of  script that is using
the database connection (it uses SQLite but would apply mutatis mutandis to
another database). I have restored multi-threading and all works smoothly. 

If this is the best way to do this, it might be worth a mention in the next
release of the documentation. I suppose ideally application code shouldn’t
use _apache.

from mod_python import apache

import _apache

import re

import sqlite

import userdatabase

import sys


users = None

apache.log_error ("userHandler initialising", apache.APLOG_INFO)


def beginDB (req):

        _apache._global_lock(req.server, None, 0)

            global users

            if not users:



                                                defaultUser =
req.get_options ()["defaultUser"]


                                                defaultUser = "guest"

                                    users = userdatabase.UserDatabase\

(req.get_options ()["dbFile"],

                                                077, autocommit=1),


                                    (type, value, tb) = sys.exc_info()

                                    apache.log_error ("Cannot access user
database: %s (%s)" % (type, value),



def endDB (req):

        _apache._global_unlock(req.server, None, 0)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://modpython.org/pipermail/mod_python/attachments/20040707/de7dd43f/attachment-0001.html

More information about the Mod_python mailing list