下面是一个例子,给出一个大的cahce的缓冲输出#Yeild
def a_big_cache():
mystr= []
for i in xrange(100):
mystr.append("{}".format(i))
return mystr
my_fat_cache = a_big_cache()
def get_in_chunks(next_chunk_size):
output =[]
counter = 0
for element in my_fat_cache:
counter += 1
output.append(element)
if counter == next_chunk_size:
counter = next_chunk_size
next_chunk_size+= next_chunk_size
yield output
del output[:]
r = get_in_chunks(10)
print next(r)
print next(r)
输出为
['0'、'1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9']
['10'、'11'、'12'、>;'13'、'14'、'15'、'16'、'17'、'18'、'19']