控制架子使用的内存
我在Python中使用shelve来处理内存中装不下的巨大字典,并实现持久化。
在运行我的代码时,需要频繁检索和插入到字典中的随机位置,我注意到搁置仅使用了 4GB 可用内存的大约 3%。这导致我的代码运行速度变慢,因为需要从磁盘进行更多的读/写操作。
有什么方法可以让搁架使用更多的可用内存(比如~50%),从而使内存中的命中次数更高?
I am using shelve in Python to handle a huge dictionary that would not fit in the memory, as well as to achieve persistence.
While running my code, which requires frequent retrieval and insertion into random locations in the dictionary, I noticed that the shelve is using only about 3% of the 4GB available memory. This is causing my code to run slower, as there would be greater number of read/writes from the disk required.
Is there any way to make the shelve use greater amount of available memory (say ~50%), so that number of hits in the memory are higher?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑与 Python 集成良好的 ZODB 或其他键值存储。任何可以搁置/腌制的对象都可以进入 ZODB。搁置模块似乎并不是为这种性能或一致性而设计的。
http://www.ibm.com/developerworks/aix/library/au- zodb/
您还可以使用 SQL 解决方案,使用 pickle 模块将该对象序列化为文本或反序列化为文本。它是 Shelve 和 ZODB 的核心。如果您确实喜欢冒险,可以尝试内存 SQLite 数据库。 SQLite 捆绑在 Python 中。
Consider ZODB or other key-value stores that integrate well with Python. Any object that can be shelved/pickled can go into ZODB. The shelve module doesnt seem to be designed for that kind of performance or consistency.
http://www.ibm.com/developerworks/aix/library/au-zodb/
You can also go for an SQL solution, with the pickle module to serialize/unserialize that object to and from text. It is the heart of Shelve and ZODB. If you are feeling really adventurous, you can try an in-memory SQLite DB. SQLite is bundled in Python.
我也强烈推荐 ZODB。您可以将搁置数据库移植到 ZODB 数据库,如下所示:
I also highly recommend ZODB. You can port your shelve database to a ZODB database like this: