|
Pythomnic » Documentation » Persistent storage (concurrency issues)
Like any other concurrent database BerkeleyDB also suffers from locking problems,
and like many it resolves deadlocks by aborting one of the conflicting
transactions. This is one other thing your application code should be aware
of. Unless you lock the safe exclusively (more on this below), there is
a chance that upon a concurrent write access even the simplest
pmnc_safe["foo"] = "bar"
will throw a DBLockDeadlockError which needs to be handled in the following
manner:
while True:
try:
pmnc_safe["foo"] = "bar"
except db.DBLockDeadlockError:
continue
else:
break
To resolve both of the mentioned problems, pmnc_safe supports advisory
interlocking across different threads. A safe could be locked exclusively:
pmnc_safe.acquire()
try:
# this thread has exclusive access to the safe,
# no other thread could interfere or deadlock
pmnc_safe["foo"] = "bar"
pmnc_safe["biz"] = "baz"
finally:
pmnc_safe.release()
or in shared mode:
pmnc_safe.acquire_shared()
try:
# other threads could access the safe, but none
# of them could have acquired exclusive lock
# as soon as this access is read-only, you don't
# have to worry about the deadlocks
assert "foo" in pmnc_safe and "biz" in pmnc_safe
finally:
pmnc_safe.release_shared()
This approach looks like a sufficient replacement to serializable transactions
whenever the module enforces interlocking policy as necessary.
|
Features Download Documentation Tutorial |