bsddb3 locking questions
Eric S. Johansson
esj at harvee.org
Sun Jan 1 12:37:17 EST 2006
man, I'm in really bad form replying to myself twice but I'me solved the
problem at least in a simple form.
Eric S. Johansson wrote:
> Eric S. Johansson wrote:
>> are there any simple examples of how to do record locking with bsddb3?
#!/usr/bin/python
from bsddb import db # the Berkeley db data base
import sys, os, time
# some helpers fcns
def countdown(id, count):
limit = 0
while limit < count:
print id, limit,
limit = limit+1
# fork into 2 instances and collide
pid = os.fork()
# Part 1: Create database and insert 4 elements
#
filename = 'fruit'
# Get an instance of BerkeleyDB
db_env = db.DBEnv()
db_env.set_lk_detect(db.DB_LOCK_YOUNGEST)
db_env.open("/tmp/bsddb3",db.DB_INIT_LOCK|db.DB_CREATE| db.DB_INIT_MPOOL)
# force a collision
time.sleep(2)
an_id = db_env.lock_id()
print pid, an_id
fruitDB = db.DB(db_env)
# Create a database in file "fruit" with a Hash access method
# There are also, B+tree and Recno access methods
fruitDB.open(filename, None, db.DB_HASH, db.DB_CREATE)
# Print version information
print '\t', pid, db.DB_VERSION_STRING
while(1):
try:
lock = db_env.lock_get(an_id, "anytid", db.DB_LOCK_WRITE,
db.DB_LOCK_NOWAIT)
break
except Exception,error:
#print error
pass
print pid, "here"
# force another one
time.sleep(2)
# Insert new elements in database
fruitDB.put("apple","red")
fruitDB.put("orange","orange")
fruitDB.put("banana","yellow")
fruitDB.put("tomato","red")
db_env.lock_put(lock)
print pid, "there"
# Close database
fruitDB.close()
# Part 2: Open database and write its contents out
#
fruitDB = db.DB(db_env)
# Open database
# Access method: Hash
# set isolation level to "dirty read (read uncommited)"
fruitDB.open(filename, None, db.DB_HASH, db.DB_DIRTY_READ)
# get database cursor and print out database content
cursor = fruitDB.cursor()
rec = cursor.first()
while rec:
print rec
rec = cursor.next()
fruitDB.close()
----------
still unanswered, under what conditions do you change the second
argument of lock_get? Is that a simple lock identifier within the
context of the lock ID so that you can lock different records? Anyway,
this locking is close enough for what I need to do.
---eric
More information about the Python-list
mailing list