Skip to content

Instantly share code, notes, and snippets.

@lepistone
Last active November 6, 2015 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepistone/8ebe0378376c95832e9b to your computer and use it in GitHub Desktop.
Save lepistone/8ebe0378376c95832e9b to your computer and use it in GitHub Desktop.
# second version: we use GeneratorExit to stop
# no more need for a method, hence no need for a class
def accumulator(batch_size):
try:
while True:
batch = []
for c in range(batch_size):
received = yield
batch.append(received)
print('flush', batch)
except GeneratorExit:
print('flush', batch)
it = accumulator(batch_size=3)
next(it) # prime it
for i in range(10):
it.send(i)
it.close() # raises GeneratorExit inside the generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment