开发者

Is shelve in Python thread safe?

Is shelve in Python used for data persistence thre开发者_JAVA百科ad safe? If not, what's a good alternative?


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/


Threads = # amount of threads

thread_moment = [False for _ in range(Threads)] 

def job(x):  # x would be the index of the thread  

    lock.aquire()

    # open/edit/update/close your shelve file

    thread_moment[x] = True 

    lock.release()

    while True:
        if all(thread_moment) == True:
            thread_moment = [False for _ in range(threads)]
            break
        else:
            time.sleep(1)

    # carry on with your script
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜