weakref的使用场景——实现缓存
来源:互联网 发布:锐捷官网 for mac 编辑:程序博客网 时间:2024/06/07 15:53
The weakref module allows the Python programmer to create weak references to objects.
In the following, the term referent means the object which is referred to by a weak reference.
A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it.
A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping.
For example, if you have a number of large binary image objects, you may wish to associate a name with each. If you used a Python dictionary to map names to images, or images to names, the image objects would remain alive just because they appeared as values or keys in the dictionaries. The WeakKeyDictionary and WeakValueDictionary classes supplied by the weakref module are an alternative, using weak references to construct mappings that don’t keep objects alive solely because they appear in the mapping objects.If, for example, an image object is a value in a WeakValueDictionary, then when the last remaining references to that image object are the weak references held by weak mappings, garbage collection can reclaim the object, and its corresponding entries in weak mappings are simply deleted.
WeakKeyDictionary and WeakValueDictionary use weak references in their implementation, setting up callback functions on the weak references that notify the weak dictionaries when a key or value has been reclaimed by garbage collection. WeakSet implements the set interface, but keeps weak references to its elements, just like a WeakKeyDictionary does.
Most programs should find that using one of these weak container types is all they need – it’s not usually necessary to create your own weak references directly. The low-level machinery used by the weak dictionary implementations is exposed by the weakref module for the benefit of advanced uses.
In the following, the term referent means the object which is referred to by a weak reference.
A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it.
A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping.
For example, if you have a number of large binary image objects, you may wish to associate a name with each. If you used a Python dictionary to map names to images, or images to names, the image objects would remain alive just because they appeared as values or keys in the dictionaries. The WeakKeyDictionary and WeakValueDictionary classes supplied by the weakref module are an alternative, using weak references to construct mappings that don’t keep objects alive solely because they appear in the mapping objects.If, for example, an image object is a value in a WeakValueDictionary, then when the last remaining references to that image object are the weak references held by weak mappings, garbage collection can reclaim the object, and its corresponding entries in weak mappings are simply deleted.
WeakKeyDictionary and WeakValueDictionary use weak references in their implementation, setting up callback functions on the weak references that notify the weak dictionaries when a key or value has been reclaimed by garbage collection. WeakSet implements the set interface, but keeps weak references to its elements, just like a WeakKeyDictionary does.
Most programs should find that using one of these weak container types is all they need – it’s not usually necessary to create your own weak references directly. The low-level machinery used by the weak dictionary implementations is exposed by the weakref module for the benefit of advanced uses.
- weakref的使用场景——实现缓存
- 缓存使用场景的理解
- [Python标准库]weakref——对象的非永久引用
- 关于weakref的用法
- 各级缓存的使用场景以及限制
- Redis缓存的具体使用场景
- CSS——!important的使用场景
- KVO的实现原理及使用场景
- 【宏观】缓存的应用场景
- 【分布式缓存】——-基于redis分布式缓存的实现
- 【分布式缓存】——-基于redis分布式缓存的实现
- 各种缓存技术及使用场景
- 数字图像识别——一种场景的实现
- 关于文件读写缓存的问题(flush的使用场景)
- dubbo总结(2)——dubbo的使用场景
- C++笔记——虚析构函数的使用场景
- dubbo总结(2)——dubbo的使用场景
- dubbo总结(2)——dubbo的使用场景
- 应用HttpClient来对付各种顽固的WEB服务器
- this指针
- Android Listener侦听的N种写法
- android NDK开发、编译、调试环境搭建与操作入门
- java时间差
- weakref的使用场景——实现缓存
- iOS中时间/日期处理NSDate的常用接口操作
- IOS中的frame 和 tableView的滑动问题
- hdu1398
- 解决Cannot load AMD 64-bit .dll
- objective c中的数组和简单操作(四)
- 线段树 POJ 3468 A Simple Problem with Integers
- Winform 树listView的拖动排序
- 2.2 Hadoop Configuration详解