Skip to content

Instantly share code, notes, and snippets.

@harishvc
Last active November 30, 2015 04:16
Show Gist options
  • Save harishvc/9f11882831f8022a8500 to your computer and use it in GitHub Desktop.
Save harishvc/9f11882831f8022a8500 to your computer and use it in GitHub Desktop.
#Reference: http://pythoncentral.io/python-generators-and-yield-keyword/
from time import sleep
def hold_client(name):
yield 'Hello, %s! You will be connected soon' % name
yield 'Dear %s, could you please wait a bit.' % name
yield 'Sorry %s, we will play a nice music for you!' % name
yield '%s, your call is extremely important to us!' % name
yield '%s, please call back latter' % name
# putting it all together
def example1(name,duration):
result = hold_client(name)
response_time = duration #seconds
try:
while True:
print(next(result))
sleep(response_time)
except StopIteration:
#pass statement does nothing, silently ignored and can be used is as a place-holder
pass
finally:
#delete generator
del result
example1("Harish", 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment