Python订购了垃圾回收字典? [英] Python ordered garbage collectible dictionary?

查看:88
本文介绍了Python订购了垃圾回收字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Python程序具有确定性,因此我在代码中一直广泛使用 OrderedDicts .不幸的是,今天在调试内存泄漏时,我发现OrderedDicts有一个自定义的 __ del __ 方法,每当有一个循环时它们就无法收集.不幸的是,文档中没有关于此的警告.

I want my Python program to be deterministic, so I have been using OrderedDicts extensively throughout the code. Unfortunately, while debugging memory leaks today, I discovered that OrderedDicts have a custom __del__ method, making them uncollectable whenever there's a cycle. It's rather unfortunate that there's no warning in the documentation about this.

那我该怎么办?Python标准库中是否有任何可与gc配合使用的确定性字典?我真的很讨厌不得不自己动手,尤其是在像这样的愚蠢的单行函数上.

So what can I do? Is there any deterministic dictionary in the Python standard library that plays nicely with gc? I'd really hate to have to roll my own, especially over a stupid one line function like this.

此外,这是我应该提交错误报告的原因吗?我不熟悉Python库的过程以及它们认为的错误.

Also, is this something I should file a bug report for? I'm not familiar with the Python library's procedures, and what they consider a bug.

看来,这是一个已知错误,已在2010年修复.我安装了2.7的旧版本.我猜最好的方法是只包含一个猴子补丁,以防用户碰巧正在运行像我这样的损坏版本.

It appears that this is a known bug that was fixed back in 2010. I must have somehow gotten a really old version of 2.7 installed. I guess the best approach is to just include a monkey patch in case the user happens to be running a broken version like me.

推荐答案

如果__del__方法的存在对您有问题,只需将其删除:

If the presence of the __del__ method is problematic for you, just remove it:

>>> import collections
>>> del collections.OrderedDict.__del__

您将能够在参考周期中使用OrderedDicts.删除后,您将失去让OrderedDict释放所有资源的机会.

You will gain the ability to use OrderedDicts in a reference cycle. You will lose having the OrderedDict free all its resources immediately upon deletion.

这篇关于Python订购了垃圾回收字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆