我将gae1.3.5devserversdk与Python结合使用。当我取消注释这行代码时,每次尝试运行测试套件时,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)
我没有得到任何崩溃或追踪。。。devserver完全没有响应。你知道吗
值得注意的是,另一个函数也使用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()
没有像我预期的那样工作吗?你知道吗
fetch(1000)
将始终返回前1000个结果。你知道吗您可能想尝试使用cursors,如下所示:
相关问题 更多 >
编程相关推荐