Ecco un semplice impaginatore che divide l'esecuzione della query in query impaginate.
from itertools import count
class PaginatedCursor(object):
def __init__(self, cur, limit=100):
self.cur = cur
self.limit = limit
self.count = cur.count()
def __iter__(self):
skipper = count(start=0, step=self.limit)
for skip in skipper:
if skip >= self.count:
break
for document in self.cur.skip(skip).limit(self.limit):
yield document
self.cur.rewind()
...
cur = collection.find({'is_timeline_valid': True})
...
for doc in PaginatedCursor(cur, limit=100):
...