python - 在 Python 中智能缓存昂贵的对象

标签 python caching memory-management

我有一个按顺序排列的图像目录。通常我的代码将使用来自图像的连续子集的数据(例如图像 5-10),访问这些的简单选项是:

  1. 使用在需要时加载图像并读取我的数据(例如像素值)的方法创建包装器对象。这几乎没有内存开销,但会很慢,因为它每次都需要加载每个图像。

  2. 将所有图像存储在内存中。这会很快,但显然我们可以存储多少图像是有限制的。

我想找到:

  • 我可以定义如何读取与索引或路径相对应的图像,然后允许我访问的一些方法,比如 magic_image_collection[index] 而我不必担心它是否是将返回内存中的对象或重新读取它。理想情况下,这会在内存中保留适当的图像或 n 个最近访问的图像。

最佳答案

您可以扩展默认字典并使用 __missing__ 方法在缺少键的情况下调用加载函数:

class ImageDict(dict):
    def __missing__(self, key):
        self[key] = img = self.load(key)
        return img
    def load(self, key):
        # create a queue if not exist (could be moved to __init__)
        if not hasattr(self, '_queue'):
            self._queue = []
        # pop the oldest entry in the list and the dict
        if len(self._queue) >= 100:
            self.pop(self._queue.pop(0))
        # append this key as a newest entry in the queue
        self._queue.append(key)
        # implement image loading here and return the image instance
        print 'loading', key
        return 'Image for %s' % key

和输出(只有当 key 不存在时才会加载。)

>>> d = ImageDict()
>>> d[3]
loading 3
'Image for 3'
>>> d[3]
'Image for 3'
>>> d['bleh']
loading bleh
'Image for bleh'
>>> d['bleh']
'Image for bleh'

一种演变是仅存储字典中的最后 N 个元素,并清除最旧的条目。您可以通过保留用于排序的键列表来实现它。

关于python - 在 Python 中智能缓存昂贵的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714358/

相关文章:

python - 使用字符串在python中调用导入的函数

caching - 在对象存储中存储具有 Control-Cache header 的对象是无法实现的

java - Java堆内存是否根据RAM变化?

c++ - 有unique_from_this()吗?或者如何从继承自 enable_shared_from_this 的类返回 unique_ptr

memory - 在什么情况下静态分配比动态分配好?

python - 有没有办法用 less "nesting"写?

python - 创建一个字典并添加一个集合作为它的值

python - android中带有opencv的kivy相机应用程序显示黑屏

google-chrome - 为什么 Chrome 不缓存响应,即使存在缓存控制 header ?

caching - Redis mget 与 get