NDB在长时间请求期间不清除内存

2024-10-01 05:06:48 发布

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

我目前正在将一个长时间运行的作业卸载到TaskQueue中,以计算数据存储中NDB实体之间的连接。在

基本上,这个队列处理多个实体键列表,它们将通过GetConnectedNodes节点中的node_in_connected_nodes函数与另一个query相关:

class GetConnectedNodes(object):
"""Class for getting the connected nodes from a list of nodes in a paged way"""
def __init__(self, list, query):
    # super(GetConnectedNodes, self).__init__()
    self.nodes = [ndb.model.Key('Node','%s' % x) for x in list]
    self.cursor = 0
    self.MAX_QUERY = 100
    # logging.info('Max query - %d' % self.MAX_QUERY)
    self.max_connections = len(list)
    self.connections = deque()
    self.query=query

def node_in_connected_nodes(self):
    """Checks if a node exists in the connected nodes of the next node in the 
       node list.
       Will return False if it doesn't, or the list of evidences for the connection
       if it does.
       """
    while self.cursor < self.max_connections:
        if len(self.connections) == 0:
            end = self.MAX_QUERY
            if self.max_connections - self.cursor < self.MAX_QUERY:
                end = self.max_connections - self.cursor
            self.connections.clear()
            self.connections = deque(ndb.model.get_multi_async(self.nodes[self.cursor:self.cursor+end]))

        connection = self.connections.popleft()
        connection_nodes = connection.get_result().connections

        if self.query in connection_nodes:
            connection_sources = connection.get_result().sources
            # yields (current node index in the list, sources)
            yield (self.cursor, connection_sources[connection_nodes.index(self.query)])
        self.cursor += 1

在 这里,Node有一个重复属性connections,它包含一个具有其他Node密钥ID的数组,以及与该给定连接匹配的sources数组。在

所得结果储存在一个blobstore中。在

现在我遇到的问题是,在一个连接函数的迭代之后,内存没有被清除。下面的日志显示了AppEngine在创建新的GetConnectedNodes实例之前使用的内存:

^{pr2}$

除了一些波动之外,内存一直在增加,即使之前的值都没有被访问。我发现很难调试这个或者弄清楚我是否在某个地方有内存泄漏,但我似乎已经把它追溯到了那个类。谢谢你的帮助。在


Tags: theinselfnodeifconnectionqueryconnections
3条回答

我们也有类似的问题(长时间运行的请求)。 我们通过关闭默认的ndb缓存来解决这些问题。 你可以阅读更多关于它的here

你可以打电话来gc.收集()在每个请求开始时。在

在我们的例子中,这是由启用AppEngine Appstats引起的。在

禁用后,内存消耗恢复正常。在

相关问题 更多 >