Skip to content

Instantly share code, notes, and snippets.

@harishvc
Last active November 30, 2015 04:35
Show Gist options
  • Save harishvc/20e0e44cfd9416d2acea to your computer and use it in GitHub Desktop.
Save harishvc/20e0e44cfd9416d2acea to your computer and use it in GitHub Desktop.
Read small chunks from big buffer
#Reference: http://pythoncentral.io/python-generators-and-yield-keyword/
#Read small chunks from big buffer
def buffered_read(input):
#Read a big chunk of data
buffer = input
#Return small chunks on demand
for small_chunk in buffer:
print("Generator invoked ..")
yield small_chunk
def example2():
result = buffered_read("12345")
for x in result:
print(x)
example2()
#output
Generator invoked ..
1
Generator invoked ..
2
Generator invoked ..
3
Generator invoked ..
4
Generator invoked ..
5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment