Last active
November 6, 2015 09:34
-
-
Save lepistone/8ebe0378376c95832e9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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