我正在使用带有 Python 的 GAE 1.3.5 devserver SDK。当我取消注释这行代码时,每次我尝试运行我的测试套件时,GAEUnit 都会挂起:
dep_arc_tail_q = db.GqlQuery("SELECT * FROM DependencyArcTail WHERE courses = :1", course)
#problem line
modelutils.applyToResultsOfQuery(lambda tails : modelutils.removeCourseFromTails(course, tails), dep_arc_tail_q)
modelutils
方法:
def applyToResultsOfQuery(func, q):
results = q.fetch(1000)
while results:
func(results)
results = q.fetch(1000)
def removeCourseFromTail(course, tail):
# tail = DependencyArcTail.get(key_tail)
if not course in tail.courses:
return
if len(tail.courses) == 1:
DependencyArcTail.delete(tail)
return
tail.courses.remove(course)
def removeCourseFromTails(course, tails):
''' Removes `course` from a collection of `tails` '''
removeThisCourseFromTail = functools.partial(removeCourseFromTail, course)
map(removeThisCourseFromTail, tails)
我没有遇到任何崩溃或回溯……开发服务器完全没有响应。
值得注意的是,另一个函数也用于modelutils.applyToResultsOfQuery
删除模型:
def _deleteType(kind):
q = kind.all(keys_only=True)
deleteResultsOfQuery(q)
def deleteResultsOfQuery(q):
applyToResultsOfQuery(db.delete, q)
这些方法的测试运行良好,让我认为问题不在于applyToResultsOfQuery
.
以下是使用的模型:
class Course(db.Model):
dept_code = db.StringProperty(required=True)
number = db.IntegerProperty(required=True)
title = db.StringProperty(multiline=True) # not sure that this should be multiline...
pickled_pre_reqs = db.StringProperty(multiline=True)
# the unparsed pre req phrase (like "CS 2110 or 1110")
unparsed_pre_reqs = db.StringProperty()
# the full value of the Note part of the course catalog listing
full_description = db.TextProperty()
# the page on which this course is described
course_catalog_url = db.LinkProperty()
parse_succeeded = db.BooleanProperty()
# true if this course has had its dependency graph built
graph_built = db.BooleanProperty()
tailsMemberOf = db.ListProperty(db.Key)
def getPreReqs(self):
return pickle.loads(str(self.pickled_pre_reqs))
def __repr__(self):
if self.dept_code == "UNKNOWN":
return "Unknown course"
return "%s %s: %s" % (self.dept_code, self.number, self.title)
# just use __dict__?
def __attrs(self):
return (self.dept_code, self.number, self.title, self.pickled_pre_reqs, self.full_description, self.unparsed_pre_reqs, self.course_catalog_url)
def __eq__(self, other):
return isinstance(other, Course) and self.__attrs() == other.__attrs()
def __hash__(self):
return hash(self.__attrs())
class DependencyArcTail(db.Model):
''' A list of courses that is a pre-req for something else '''
courses = db.ListProperty(db.Key) # can this be changed to Course.key?
''' a list of heads that reference this one '''
forwardLinks = db.ListProperty(db.Key)
def __repr__(self):
return "DepArcTail %d: courses='%s' forwardLinks='%s'" % (id(self), getReprOfKeys(self.courses), getIdOfKeys(self.forwardLinks))
def __eq__(self, other):
return isinstance(other, DependencyArcTail) and set(self.courses) == set(other.courses) and set(self.forwardLinks) == set(other.forwardLinks)
def __hash__(self):
return hash((frozenset(self.courses), frozenset(self.forwardLinks)))
我还能在这里做错什么?
更新:似乎在未注释该行的情况下运行测试后,整个开发服务器崩溃。如果我在那之后导航到非测试页面,我会得到 500 分。我不确定从中得出什么结论。
更新2:如果我摆脱modeutils
,并以另一种方式写出来,它可以正常工作:
dep_arc_tail_q = db.GqlQuery("SELECT * FROM DependencyArcTail WHERE courses = :1", course)
# modelutils.applyToResultsOfQuery(lambda tails : modelutils.removeCourseFromTails(course, tails), dep_arc_tail_q)
results = dep_arc_tail_q.fetch(1000)
# while results:
for tail in results:
modelutils.removeCourseFromTail(course, tail)
# results = dep_arc_tail_q.fetch(1000)
但是,如果我更改评论,它会再次失败:
dep_arc_tail_q = db.GqlQuery("SELECT * FROM DependencyArcTail WHERE courses = :1", course)
# modelutils.applyToResultsOfQuery(lambda tails : modelutils.removeCourseFromTails(course, tails), dep_arc_tail_q)
results = dep_arc_tail_q.fetch(1000)
while results:
for tail in results:
modelutils.removeCourseFromTail(course, tail)
results = dep_arc_tail_q.fetch(1000)
我得到一个无限循环吗?fetch()
没有按我的预期工作?