def ResultIter(cursor, arraysize=1000):
    'An iterator that uses fetchmany to keep memory usage down'
    while True:
        results = cursor.fetchmany(arraysize)
        if not results:
            break
        for result in results:
            yield result
An iterator that uses fetchmany to keep memory usage down
recipes/Python/137270_Use_generators_fetching_large_db_record/recipe-137270.py