【发布时间】:2011-03-04 02:52:25
【问题描述】:
Python 中的 shelve 用于数据持久化线程安全吗?如果没有,有什么好的选择?
【问题讨论】:
标签: python thread-safety shelve
Python 中的 shelve 用于数据持久化线程安全吗?如果没有,有什么好的选择?
【问题讨论】:
标签: python thread-safety shelve
来自standard library documentation about the Shelve module, under the heading Restrictions:
搁置模块不支持 并发读/写访问 搁置的物品。 (多 同时读取访问是安全的。)
我会假设它可能依赖于实现,在这种情况下,为了确定,我会得出结论,它肯定不是线程安全的。
【讨论】:
替代品:ZODB
【讨论】:
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
【讨论】: