如何打破CPython中的直接引用循环

2024-06-08 04:42:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在CPython中,我有两种类型的对象,它们彼此紧密相连。在

#include <Python.h>
#include <structmember.h>

typedef struct pyt PYT;
struct pyt { PyObject_HEAD PYT *other; };

static void dealloc (PYT *self) {
    Py_CLEAR(self->other);
    printf("dealloc object at %p\n", self);
    PyObject_GC_Del(self);
}

static PyTypeObject Pyt2Type = {
    PyObject_HEAD_INIT(NULL)
    0, "pyt.Pyt2", sizeof(PYT), 0,
    (destructor) dealloc
};

static PyObject * new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
    PYT *self = PyObject_GC_New(PYT, type);
    if (!self) return NULL;
    self->other = PyObject_GC_New(PYT, &Pyt2Type);
    if (!self->other) { Py_DECREF(self); return NULL; }
    return Py_INCREF(self), self->other->other = self, (PyObject *) self;
}

static PyTypeObject Pyt1Type = {
    PyObject_HEAD_INIT(NULL)
    0, "pyt.Pyt1", sizeof(PYT), 0,
    (destructor) dealloc
};

static int traverse (PYT *self, visitproc visit, void *arg) {
    Py_VISIT(self->other);
    return 0;
}

static int clear (PYT *self) {
    Py_CLEAR(self->other);
    return 0;
}

static PyMemberDef members[] = {
    {"other", T_OBJECT, offsetof(PYT, other), RO, "other"},
    { NULL }
};

static PyMethodDef methods[] = {{ NULL }};

PyMODINIT_FUNC initpyt ( void ) {
    PyObject* m;

    Pyt1Type.tp_flags = Pyt2Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
    Pyt1Type.tp_traverse = Pyt2Type.tp_traverse = (traverseproc) traverse;
    Pyt1Type.tp_clear = Pyt2Type.tp_clear = (inquiry) clear;
    Pyt1Type.tp_members = Pyt2Type.tp_members = members;
    Pyt1Type.tp_new = new;

    if (PyType_Ready(&Pyt1Type) < 0) return;
    if (PyType_Ready(&Pyt2Type) < 0) return;

    m = Py_InitModule("pyt", methods);

    Py_INCREF(&Pyt1Type), PyModule_AddObject(m, "Pyt", (PyObject *) &Pyt1Type);
}

使用我的测试脚本

^{pr2}$

我得到的输出是

<pyt.Pyt1 object at 0x7fbc26540138> 3
<pyt.Pyt2 object at 0x7fbc26540150> 3

最后不会删除这些对象,因为每个对象都保留对另一个对象的引用,从而创建一个闭合循环。在另一段代码中,我使用了一种方法,即只保留对象,直到两个对象的refcount都为0,我怀疑这是一种不好的做法。我还没试过用垃圾回收器来收集,但我现在还没用过。在

这里出什么问题了?我错过了什么?在


Tags: 对象pyselfreturnstaticpytnullgc
3条回答

关于(大多数)垃圾收集语言需要注意的一件重要事情是,不能保证在对象变得不可访问时立即删除对象。一旦一个对象变得不可访问,它将完全由垃圾回收器决定何时释放相关的资源,如果没有内存压力,这可能会延迟到程序结束时。在

如果不为链接类设置__del__方法,那么垃圾收集器应该可以正常工作。它不会立即清理对象,因为检测引用循环的函数比简单的引用计数开销更大,因此很少运行。在

使用纯python类的示例

import gc
import weakref

class Obj(object): pass

x = Obj()
y = Obj()

x.y = y, y.x = x

ref = weakref.ref(x)

print(ref())
del x, y
print(ref())
gc.collect()
print(ref())

输出:

^{pr2}$

您可以使用弱引用来实现这一点(请参见weakref模块)。但通常依靠垃圾回收器会更好。有可能其他人会创建一个涉及到您的对象的大型引用循环,然后您无论如何都将依赖于GC,因此您也可以将其用于简单的情况。在

请解释一下你所说的“严重失败”是什么意思

好吧,我终于找到了我的问题。我没有用PyObject_GC_Track开始跟踪。在

使用垃圾收集器时,Python需要执行以下步骤:

  • Py_TPFLAGS_HAVE_GC添加到tp_flags
  • 添加tp_traversetp_clear函数(如果需要)
  • 使用PyObject_GC_New或类似函数创建对象
  • 在完全初始化的对象上调用PyObject_GC_Track
  • 使用PyObject_GC_Del或类似函数删除对象

所以在这里修改new函数就足够了。在

static PyObject * new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
    PYT *self = PyObject_GC_New(PYT, type);
    if (!self) return NULL;
    self->other = PyObject_GC_New(PYT, &Pyt2Type);
    if (!self->other) { Py_DECREF(self); return NULL; }
    self->other->other = (Py_INCREF(self), self);
    PyObject_GC_Track((PyObject *) self);
    PyObject_GC_Track((PyObject *) self->other);
    return (PyObject *) self;
}

输出为

^{pr2}$

相关问题 更多 >

    热门问题