Is shelve in Python used for data persistence thread safe? If not, what's a good alternative?
They are thread-safe as long as you don't disable the GIL in C code for the thread.
Python is not thread-safe, and was originally designed with something called the GIL, or Global Interpreter Lock, that ensures processes are executed serially on a computer's CPU. On the surface, this means Python programs cannot support multiprocessing.
Source code: Lib/shelve.py. A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle.
The print() function is a built-in function for printing a string on stdout and is not thread-safe.
From the standard library documentation about the Shelve module, under the heading Restrictions:
The shelve module does not support concurrent read/write access to shelved objects. (Multiple simultaneous read accesses are safe.)
I would assume that it's probably implementation dependent and in which case, in order to be sure, I would conclude that it certainly is not thread safe.
Alternatives: ZODB
http://www.zodb.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With