过期的查询和appengine

时间:2011-09-17 16:42:32

标签: python google-app-engine

在任务中,我正在使用查询迭代一组项目。从查询中提取每个实体后,我也在执行URL请求。迭代了大量这些项后,我看到以下错误:

BadRequestError: The requested query has expired. Please restart it with the last cursor to read more results.

创建查询后的租约是多少?

3 个答案:

答案 0 :(得分:7)

此问题可能会对您的问题有所了解:https://code.google.com/p/googleappengine/issues/detail?id=4432

  

即使离线请求目前可以长达10分钟(和   后台实例可以永久存在)数据存储区查询仍然可以   只活30秒。我们计划改善这一点,但是因为a   数据的“一致”视图仅在一段时间内保留   时间,查询可以持续多长时间的上限(<   10分钟)。

...

  

请考虑从中获取批次,而不是运行单个长查询   使用查询游标的查询。

答案 1 :(得分:1)

我写了一个简单的帮助器 - 你用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)

答案 2 :(得分:0)

为序列中的每个元素创建简单的延迟任务。有一篇很好的文章说明如何以正确的方式“Background work with the deferred library”。