简体   繁体   English

Python shelve.open 上的资源暂时不可用

[英]Resource temporarily unavailable on Python shelve.open

I have a Python application that does the following:我有一个执行以下操作的 Python 应用程序:

  • Is called by another process/processes once every 2-3 minutes in order to store an object using with shelve.open(shelvefilename, flag='c') .由另一个进程/进程每 2-3 分钟调用一次,以使用with shelve.open(shelvefilename, flag='c')存储对象。
  • Is called by another process/processes many times per minute in order to read that shelve file using 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-objectshttps://docs.python.org/2/library/threading.html#lock-objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python shelve.open(...,flag ='n')不会创建一个空架子 - The python shelve.open(…,flag='n') does not create an empty shelve Shelve.open创建失败 - Shelve.open failing on creation 可以用嵌套方式调用Python的shelve.open吗? - Can Python's shelve.open be called in a nested fashion? Shelve使用“ with shelve.open”语法在Python 3.3中为hello world示例提供了AttributeError,但并非没有它 - Shelve gives AttributeError in Python 3.3 for hello world example using the “with shelve.open” syntax, but not without it Python中的子进程模块资源暂时不可用错误 - Resource temporarily unavailable error with subprocess module in Python 限制 Python 线程:资源暂时不可用 - Limit Python Threads : Resource Temporarily Unavailable Python Socket - [Errno 35] 资源暂时不可用 - Python Socket - [Errno 35] Resource temporarily unavailable dbm 35资源在OSX python 2.7上暂时不可用 - dbm 35 Resource temporarily unavailable on OSX python 2.7 virtualenvwrapper.sh: fork: 资源暂时不可用 - Python/Django - virtualenvwrapper.sh: fork: Resource temporarily unavailable - Python/Django “资源暂时不可用”后的Python多处理池恢复 - Python multiprocessing Pool recovery after “Resource temporarily unavailable”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM