这是一个yield的示例,可以提供来自大缓存的缓冲输出
#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”)