Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is shelve in Python thread safe?

Is shelve in Python used for data persistence thread safe? If not, what's a good alternative?

like image 964
Tianyang Li Avatar asked Mar 04 '11 02:03

Tianyang Li


People also ask

Is set in Python thread-safe?

They are thread-safe as long as you don't disable the GIL in C code for the thread.

Why is Python not thread-safe?

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.

What is shelve Python?

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.

Is print in Python thread-safe?

The print() function is a built-in function for printing a string on stdout and is not thread-safe.


2 Answers

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.

like image 66
Mahmoud Abdelkader Avatar answered Sep 22 '22 05:09

Mahmoud Abdelkader


Alternatives: ZODB

http://www.zodb.org/

like image 21
Andreas Jung Avatar answered Sep 21 '22 05:09

Andreas Jung