def iter_chunks(self, db, view_name, startkey=None, endkey=None, reduce=False, chunksize=1000):

        # A recursive formulation is superior but Python does not optimize tail calls
        more_to_fetch = True
        nextkey = startkey

        while more_to_fetch:

            chunk = db.view(
                view_name,
                startkey=nextkey,
                endkey=endkey,
                reduce=reduce,
                include_docs=True,
                limit=chunksize + 1 # The last doc provides the startkey for the next chunk
            )

            rows = chunk.all()

            yield [row['doc'] for row in rows[:chunksize]]

            if len(rows) <= chunksize:
                more_to_fetch = False
                nextkey = None
            else:
                nextkey = rows[-1]['key']
iterates over chunks of the raw docs of the view, for bulk processing
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/domainsync/management/commands/copy_doc_types.py/Command.iter_chunks