dbm concurrency

Chuck Swiger cswiger at mac.com
Wed Apr 23 10:40:40 EDT 2003


Clemens Hermann wrote:
> I want to use a standard unix dbm database to store user->regexp
> combinations for squid ACLs. The dbm is written from a python cgi and
> it is assured that only one write-access at a time occurs.
> On the other side the same dbm is accessed read-only from a
> C-programm. There may be many concurrent read-only accessess and also
> requests for read-access while python writes to the dbm.
> 
> Might this be a problem when the C-program requests read-only access
> while python is writing to the dbm or vice versa? If so, how can this
> be solved?

If it's a problem that the C program may cache old user/regexp 
combinations after they've been updated (and it does do), yes, you'd 
have an obvious concurrency/race-condition issue.

What you should first consider is making changes to the database atomic 
to the C program.  By this I mean, have python update a copy of the DBM 
database, not the actual one that the C code is looking at.  Once python 
has written all of the changes it wants to make, use "mv" [or 
unlink()/rename()] to swap the new database in.

The next thing is to either restart the C programs to make them re-read 
the new DB (via 'squid -k reconfigure'?), or else have them fstat the 
database file and refetch any cached results if the DB file's 
last-modified timestamp changes.  You could also look into something 
like the BerkeleyDB 4 from sleepycat.com, and what sendmail does for the 
hash or btree map types like /etc/mail/aliases or mailertable or access.

-Chuck







More information about the Python-list mailing list