Skip to content

Instantly share code, notes, and snippets.

@jaysudodeveloper
Created January 7, 2016 22:33
Show Gist options
  • Save jaysudodeveloper/db92c17dac8e29835222 to your computer and use it in GitHub Desktop.
Save jaysudodeveloper/db92c17dac8e29835222 to your computer and use it in GitHub Desktop.
# wait for incoming data to consume with a for loop
def consume():
while True:
data = yield
consumer = data # Aliase the data variable to ensure the generator stays "charged"
for i in consumer: # consume the data and return back to the while loop
print i
# Just a basic generator object for proof of concept
def in_gen(x):
for i in range(x):
yield i
consumer = consume()
data = in_gen(5)
consumer.send(None) # precharge the generator before sending data. this is a must
# as generator objescts dont start when intantiated
consumer.send(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment