【发布时间】:2012-01-23 21:42:14
【问题描述】:
在一个任务中,我正在使用一个查询迭代一组项目。从查询中获取每个实体后,我还在执行 URL 请求。遍历大量这些项目后,我看到以下错误:
BadRequestError: The requested query has expired. Please restart it with the last cursor to read more results.
创建查询后的租约是多少?
【问题讨论】:
在一个任务中,我正在使用一个查询迭代一组项目。从查询中获取每个实体后,我还在执行 URL 请求。遍历大量这些项目后,我看到以下错误:
BadRequestError: The requested query has expired. Please restart it with the last cursor to read more results.
创建查询后的租约是多少?
【问题讨论】:
这个问题可能会对您的问题有所帮助:https://code.google.com/p/googleappengine/issues/detail?id=4432
即使离线请求当前最多可以存在 10 分钟(并且 后台实例可以永远存在)数据存储查询仍然可以 只活30秒。我们计划对此进行改进,但由于 数据的“一致”视图仅保留一段时间 时间,查询可以持续多长时间有一个上限(即
...
与其运行单个长查询,不如考虑从 使用查询游标进行查询。
【讨论】:
我为此编写了一个简单的帮助程序 - 您可以使用 batch_size、查询的对象类以及处理查询中的元素的回调来调用它。
(注意,我使用的是 djangoappengine,因此使用的是 django 查询格式 - 但您可以对其进行修改以适合。)
def loop_over_objects_in_batches(batch_size, object_class, callback):
logging.info("Calling batched loop with batch_size: %d, object_class: %s, callback: %s" % (batch_size, object_class, callback))
num_els = object_class.objects.all().count()
num_loops = num_els / batch_size
remainder = num_els - num_loops * batch_size
offset = 0
while offset < num_loops * batch_size:
logging.info("Processing batch (%d:%d)" % (offset, offset+batch_size))
query = object_class.objects.all()[offset:offset + batch_size]
for q in query:
callback(q)
offset = offset + batch_size
if remainder:
logging.info("Processing remainder batch (%d:-)" % offset)
query = object_class.objects.all()[offset:]
for q in query:
callback(q)
【讨论】:
为序列中的每个元素简单地创建延迟任务。有很好的文章示例如何正确地做到这一点“Background work with the deferred library”。
【讨论】: