Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:为什么这段代码要花很长时间(无限循环?)_Python_Google App Engine_Recursion_Infinite Loop - Fatal编程技术网

Python:为什么这段代码要花很长时间(无限循环?)

Python:为什么这段代码要花很长时间(无限循环?),python,google-app-engine,recursion,infinite-loop,Python,Google App Engine,Recursion,Infinite Loop,我正在谷歌应用程序引擎中开发一个应用程序。我的方法之一是采用永不完成,这让我觉得它陷入了一个无限循环。我已经盯着它看了,但是想不出来 免责声明:我正在使用运行我的测试。也许它的行为很奇怪 这就是有问题的函数: def _traverseForwards(course, c_levels): ''' Looks forwards in the dependency graph ''' result = {'nodes': [], 'arcs': []} if c_leve

我正在谷歌应用程序引擎中开发一个应用程序。我的方法之一是采用永不完成,这让我觉得它陷入了一个无限循环。我已经盯着它看了,但是想不出来

免责声明:我正在使用运行我的测试。也许它的行为很奇怪

这就是有问题的函数:

def _traverseForwards(course, c_levels):
    ''' Looks forwards in the dependency graph '''
    result = {'nodes': [], 'arcs': []}

    if c_levels == 0:
        return result

    model_arc_tails_with_course = set(_getListArcTailsWithCourse(course))
    q_arc_heads = DependencyArcHead.all()

    for model_arc_head in q_arc_heads:
        for model_arc_tail in model_arc_tails_with_course:
            if model_arc_tail.key() in model_arc_head.tails:
                result['nodes'].append(model_arc_head.sink)
                result['arcs'].append(_makeArc(course, model_arc_head.sink))

                # rec_result = _traverseForwards(model_arc_head.sink, c_levels - 1)

                # _extendResult(result, rec_result)

    return result
起初,我认为这可能是一个递归错误,但我注释掉了递归,问题仍然存在。如果使用
c_levels=0
调用此函数,它将正常运行

它引用的模型:

class Course(db.Model):
    dept_code = db.StringProperty()
    number = db.IntegerProperty()
    title = db.StringProperty()
    raw_pre_reqs = db.StringProperty(multiline=True)
    original_description = db.StringProperty()

    def getPreReqs(self):
        return pickle.loads(str(self.raw_pre_reqs))

    def __repr__(self):
        return "%s %s: %s" % (self.dept_code, self.number, self.title)

class DependencyArcTail(db.Model):
    ''' A list of courses that is a pre-req for something else '''
    courses = db.ListProperty(db.Key)

    def equals(self, arcTail):
        for this_course in self.courses:
            if not (this_course in arcTail.courses):
                return False

        for other_course in arcTail.courses:
            if not (other_course in self.courses):
                return False

        return True

class DependencyArcHead(db.Model):
    ''' Maintains a course, and a list of tails with that course as their sink '''
    sink = db.ReferenceProperty()
    tails = db.ListProperty(db.Key) 
def _makeArc(source, sink):
    return {'source': source, 'sink': sink}

def _getListArcTailsWithCourse(course):
    ''' returns a LIST, not SET 
        there may be duplicate entries
    '''
    q_arc_heads = DependencyArcHead.all()
    result = []
    for arc_head in q_arc_heads:
        for key_arc_tail in arc_head.tails:
            model_arc_tail = db.get(key_arc_tail)
            if course.key() in model_arc_tail.courses:
                result.append(model_arc_tail)

    return result
它引用的实用程序函数:

class Course(db.Model):
    dept_code = db.StringProperty()
    number = db.IntegerProperty()
    title = db.StringProperty()
    raw_pre_reqs = db.StringProperty(multiline=True)
    original_description = db.StringProperty()

    def getPreReqs(self):
        return pickle.loads(str(self.raw_pre_reqs))

    def __repr__(self):
        return "%s %s: %s" % (self.dept_code, self.number, self.title)

class DependencyArcTail(db.Model):
    ''' A list of courses that is a pre-req for something else '''
    courses = db.ListProperty(db.Key)

    def equals(self, arcTail):
        for this_course in self.courses:
            if not (this_course in arcTail.courses):
                return False

        for other_course in arcTail.courses:
            if not (other_course in self.courses):
                return False

        return True

class DependencyArcHead(db.Model):
    ''' Maintains a course, and a list of tails with that course as their sink '''
    sink = db.ReferenceProperty()
    tails = db.ListProperty(db.Key) 
def _makeArc(source, sink):
    return {'source': source, 'sink': sink}

def _getListArcTailsWithCourse(course):
    ''' returns a LIST, not SET 
        there may be duplicate entries
    '''
    q_arc_heads = DependencyArcHead.all()
    result = []
    for arc_head in q_arc_heads:
        for key_arc_tail in arc_head.tails:
            model_arc_tail = db.get(key_arc_tail)
            if course.key() in model_arc_tail.courses:
                result.append(model_arc_tail)

    return result
我是不是漏掉了一些很明显的东西,还是盖恩特做错了


另外,使此运行变慢的测试在数据存储中的任何类型的模型都不超过5个。我知道这可能很慢,但我的应用程序只执行一次,然后将其缓存。

忽略注释掉的递归,我不认为这应该是一个无限循环-您只是在有限结果集上执行一些for循环

然而,这看起来确实很慢。在整个表上循环,然后在每个嵌套循环中执行更多的数据存储查询。这类请求似乎不太可能在GAE上及时完成,除非您的表非常非常小


一些粗略的数字:

如果
H
=
dependencyard
中的实体数和
T
=每个
dependencyard
中尾部的平均数,则:

  • \u getListArcTailsWithCourse
    正在处理
    H*T
    查询(估计不足)。在“最坏”的情况下,此函数返回的
    结果将包含
    H*T
    元素
  • \u遍历所有这些结果
    H
    次,从而执行另一个H*(H*T)查询
  • 即使
    H
    T
    仅为10秒左右,您也可能要进行数千次查询。如果他们更大,那么。。。(这将忽略在取消递归调用注释时所做的任何其他查询)
简言之,如果可能的话,我认为您可能希望尝试以稍微不同的方式组织数据。我想提出一个具体的建议,但我不清楚你到底想做什么