[英]Resource temporarily unavailable on Python shelve.open
I have a Python application that does the following:我有一个执行以下操作的 Python 应用程序:
with shelve.open(shelvefilename, flag='c')
.with shelve.open(shelvefilename, flag='c')
存储对象。with shelve.open(shelvefilename, flag='r')
with shelve.open(shelvefilename, flag='r')
The problem is that at some time I get a _gdbm.error: [Errno 11] Resource temporarily unavailable
Error:问题是有时我会收到
_gdbm.error: [Errno 11] Resource temporarily unavailable
错误:
File "/path/to/myprog.py", line 755, in mymethod
with shelve.open(shelvefilename, flag='r') as shlvfile:
File "/usr/local/lib/python3.6/shelve.py", line 243, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/local/lib/python3.6/shelve.py", line 227, in __init__
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/usr/local/lib/python3.6/dbm/__init__.py", line 94, in open
return mod.open(file, flag, mode)
_gdbm.error: [Errno 11] Resource temporarily unavailable
My guess is that this happen because at some moment I have opened the shelve file both for read and write operation which is problematic by definition.我的猜测是发生这种情况是因为在某个时刻我打开了搁置文件以进行读写操作,这在定义上是有问题的。
Is there any way that I can make the update to the shelve file without disturbing the read operations?有什么方法可以在不干扰读取操作的情况下更新搁置文件?
This is more of a conceptional issue.这更多是一个观念问题。 If one process modifies data in a file while another process reads it at the same time, the result is kind of unpredictible.
如果一个进程修改文件中的数据而另一个进程同时读取它,结果是一种不可预测的。
Imagine that you read a portion of file where only half of some value has been written at this point in time.想象一下,您读取了文件的一部分,此时仅写入了某个值的一半。 The read would simply fail to parse the entry correctly and probably all subsequent entries too.
读取只会无法正确解析条目,也可能无法正确解析所有后续条目。 In other words, it will break sooner or later.
换句话说,它迟早会破裂。
I think the best approach would be to centralize the "shelve" in a single process or use a database.我认为最好的方法是将“搁置”集中在一个进程中或使用数据库。
I just ran into this issue using shelve in a small module for caching exchange rates.我刚刚在一个小模块中使用搁置来缓存汇率时遇到了这个问题。 You can solve the issue with python threading and a global
threading.Lock()
object.您可以使用 python 线程和全局
threading.Lock()
对象来解决这个问题。
from threading import Lock
import shelve
mutex = Lock()
mutex.acquire()
db = shelve.open(db_name)
# write to db
db.close()
mutex.release()
the original source for the code snippet and credit for the idea can be found @ http://georg.io/2014/06/Python-Shelve-Thread-Safety可以在@ http://georg.io/2014/06/Python-Shelve-Thread-Safety找到代码片段的原始来源和该想法的功劳
The ```threading.Lock()`` object works by blocking the other python threads until it's released. ```threading.Lock()`` 对象通过阻塞其他 python 线程来工作,直到它被释放。 see https://docs.python.org/2/library/threading.html#lock-objects
见https://docs.python.org/2/library/threading.html#lock-objects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.