Python Shelve Thread Safety

As the documentation of shelve says, shelve.Shelf objects are not thread safe:

The shelve module does not support concurrent read/write access to shelved objects.

To write to a Shelf object in an environment where multiple threads may end up writing to it, use a global threading.Lock object.

from threading import Lock
import shelve

mutex = Lock()

db =
# write to db
comments powered by Disqus