Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expired queries and appengine

Within a task, I'm iterating over a collection of items with a query. After each entity is fetched from the query, I am also performing a URL request. After iterating over a large number of these items, I'm seeing the following error:

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

What is the lease on a query once you create it?

like image 302
Dan Holman Avatar asked Sep 17 '11 16:09

Dan Holman


2 Answers

This issue might shed some light on your problem: https://code.google.com/p/googleappengine/issues/detail?id=4432

Even though offline requests can currently live up to 10 minutes (and background instances can live forever) datastore queries can still only live for 30 seconds. We plan to improve this, but since a 'consistent' view of the data is only preserved for a limit period of time, there is an upper bound to how long a query can last (which is < 10 minutes).

...

Instead of running a single long query, consider fetching batches from the query using query cursors.

like image 198
Hoff Avatar answered Oct 24 '22 09:10

Hoff


I wrote a simple helper to do this - you call it with the batch_size, the object class for the query, and the callback that handles the elements in the query.

(Note, I am using djangoappengine and therefore django query format - but you could modify it to suit.)

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)
like image 28
Stephen Brown Avatar answered Oct 24 '22 10:10

Stephen Brown