What is the most efficient way to get a fixed number of elements from a generator?
I am currently using zipand range. In this example, I take pieces of size 3 from the generator.
def f():
x = 0
while x < 21:
yield x
x += 1
g = f()
while True:
x = [i for _, i in zip(range(3), g)]
if not x:
break
print x
It is assumed that the database used provides a generator object for the query results. Than I fill an array with a fixed numpy size and process it as one batch.
source
share