在使用处理每个节点(与儿童一起)后,尝试清理会议。下面的样本代码打印了会议实例的数量 :
class Node(Base):
__tablename__ = node
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey( node.id ))
name = Column(String(50))
children = relationship("Node",
backref=backref("parent", remote_side=[id],)
)
def process_node(node, expunge=False, ident=1):
print "Node: ", "-" * ident, node, " --> ", len(session.identity_map)
for child in node.children:
process_node(child, expunge, ident + 4)
session.expunge(child)
roots = session.query(Node).filter(Node.parent == None)
for root in roots:
process_node(root, True)