[mod_python] Apache on Windows and concurrency

Iain Mackay imm at iain-mackay.org
Thu Jul 8 01:49:59 EDT 2004


Dear Nicolas
Thank you for those most helpful comments. 
As a first step I rewrote my specimen code using a Lock on a global database
connection: This is very much faster than the server locks, and at least
with a modest load seems to have no concurrency problems either.
import threading

users = None
apache.log_error ("userHandler initialising", apache.APLOG_INFO)
DBLock = threading.Lock()

def beginDB (req):
	global DBLock
        DBLock.acquire()
	global users
	if not users:
		try:
			try:
				defaultUser = req.get_options
()["defaultUser"]
			except:
				defaultUser = "guest"
			users = userdatabase.UserDatabase\
				(sqlite.connect (req.get_options
()["dbFile"],
				077, autocommit=1), defaultUser)
		except:
			(type, value, tb) = sys.exc_info()
			apache.log_error ("Cannot access user database: %s
(%s)" % (type, value),
				apache.APLOG_ERR)
        
def endDB (req):
        DBLock.release()
This is still sharing a single database connection. The issue of how much
benefit you can get from multiple connections is a complex one; in the case
of SQLite I suspect it is quite limited, because SQLite locks the entire
database for each transaction. In a production situation one might well us a
different database.
If we move to a connection-per-thread paradigm, the code becomes slightly
more complex than I like to attempt in the wee small hours, so I'll leave
that for later.
I suppose the main drift here is that on Windows one has to beware threading
issues in mod_python; but t(h)read carefully and all will be well.
Iain





More information about the Mod_python mailing list