Nobilis Nobilis - 9 days ago 8
Python Question

Google App Engine: Safe to reuse the same cursor for the same batch of results?

Say I'm iterating over a bunch of results with a cursor enabled 5 elements at a time.

Is the cursor ID for any given batch of 5 elements safe to reuse (as in an exception won't be thrown and I'll always get the same results)?

I'm carrying out some tests locally and it seems that every time I pass the same cursor to the query, I get the exact same results, but was wondering if this is a defined, consistent behaviour.

Answer

From Query Cursors (emphasis mine):

A query cursor is a small opaque data structure representing a resumption point in a query. This is useful for showing a user a page of results at a time; it's also useful for handling long jobs that might need to stop and resume. A typical way to use them is with a query's fetch_page() method. It works somewhat like fetch(), but it returns a triple (results, cursor, more). The returned more flag indicates that there are probably more results; a UI can use this, for example, to suppress a "Next Page" button or link. To request subsequent pages, pass the cursor returned by one fetch_page() call into the next.

Fundamentally the cursor simply represents the current position (index if you want) in the list of the query's results.

You can only get the same results displayed (guaranteed I mean) for a cursor only if the list of query results doesn't change.

If the list of results changes (be it because of the number of the items in the list changes or because their order in the result list changes) then the results displayed might change.