Search code examples
pythongoogle-app-enginecursor

How to reset query cursor in Appengine?


My code fetches results from the datastore in batches, and detecs when it reaches the end. How do I actually reset, or remove, the cursor, so that the next time, I start at the beginning of the query?

q = Company.all()
q.order("datetime")

company_cursor = memcache.get("company_cursor")

if company_cursor:
    q.with_cursor(start_cursor = company_cursor)

chunk_size = 5
companies = q.fetch(chunk_size)

for company in companies: 
    do_stuff(company)

if len(companies) < chunk_size:
    # This is where I want to reset, or remove, the cursor.
    memcache.add("company_cursor",company_cursor, 11000) # 10800 == 180 min.

company_cursor = q.cursor() 
memcache.set("company_cursor",company_cursor, 11000)

Solution

  • On GAE you can just stop using the cursor.

    So in your case, you'd just clear the cursor out of memcache, and you should be ok.