11

Python中的搁置用于数据持久性线程安全吗?如果没有,有什么好的选择?

4

3 回答 3

13

关于 Shelve 模块的标准库文档中,在标题 Restrictions 下

搁置模块不支持对搁置对象的并发读/写访问。(多个同时读取访问是安全的。)

我会假设它可能依赖于实现,在这种情况下,为了确定,我会得出结论,它肯定不是线程安全的。

于 2011-03-04T08:29:42.330 回答
3

替代品:ZODB

http://www.zodb.org/

于 2011-03-04T03:04:58.403 回答
-1
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
于 2021-08-12T17:48:19.630 回答