Skip to content

Instantly share code, notes, and snippets.

@ychennay
Last active December 29, 2019 20:49
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 ychennay/7a252a619fbe4528860faec8f6f6b250 to your computer and use it in GitHub Desktop.
Save ychennay/7a252a619fbe4528860faec8f6f6b250 to your computer and use it in GitHub Desktop.
Simple example highlight execution of concurrent function calls (a top level main function and a generator function), yielding control back and forth and sending messages to each other.
# helper class taken from https://www.geeksforgeeks.org/print-colors-python-terminal/
class colors:
'''Colors class:reset all colors with colors.reset; two
sub classes fg for foreground
and bg for background; use as colors.subclass.colorname.
i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable,
underline, reverse, strike through,
and invisible work with the main class i.e. colors.bold'''
reset = '\033[0m'
bold = '\033[01m'
disable = '\033[02m'
underline = '\033[04m'
reverse = '\033[07m'
strikethrough = '\033[09m'
invisible = '\033[08m'
class fg:
black = '\033[30m'
red = '\033[31m'
green = '\033[32m'
orange = '\033[33m'
blue = '\033[34m'
purple = '\033[35m'
cyan = '\033[36m'
lightgrey = '\033[37m'
darkgrey = '\033[90m'
lightred = '\033[91m'
lightgreen = '\033[92m'
yellow = '\033[93m'
lightblue = '\033[94m'
pink = '\033[95m'
lightcyan = '\033[96m'
class bg:
black = '\033[40m'
red = '\033[41m'
green = '\033[42m'
orange = '\033[43m'
blue = '\033[44m'
purple = '\033[45m'
cyan = '\033[46m'
lightgrey = '\033[47m'
def generator_function():
while True:
print(colors.fg.green, "GENERATOR\tAt start of while loop.")
input()
val = yield
print(colors.fg.green, f"GENERATOR\tfunction executing now. received {val} sent from top level function")
input()
print(colors.fg.green, f"GENERATOR\tAbout to yield {val * 10}")
input()
yield val * 10
print(colors.fg.green, f"GENERATOR\tresuming execution. Reached end of while loop.")
input()
if __name__ == "__main__":
# example code based upon https://www.bogotobogo.com/python/python_function_with_generator_send_method_yield_keyword_iterator_next.php
generator = generator_function()
value1 = next(generator)
print(colors.fg.lightred, f"TOP\tlevel function received {value1} from generator. Calling next()")
print(colors.fg.lightred, f"TOP\tlevel function sending 5.")
return_value = generator.send(5)
print(colors.fg.lightred, f"TOP\tlevel function executing again. Received {return_value} from generator")
input()
print(colors.fg.lightred, f"TOP\tlevel function calling NEXT.")
value2 = next(generator)
input()
print(colors.fg.lightred, f"TOP\tlevel function sending 100")
return_value2 = generator.send(100)
print(colors.fg.lightred, f"TOP\tlevel main function received {value2} from generator. Calling next()")
input()
''' Stdout below:
GENERATOR At start of while loop.
TOP level function received None from generator. Calling next()
TOP level function sending 5.
GENERATOR function executing now. received 5 sent from top level function
GENERATOR About to yield 50
TOP level function executing again. Received 50 from generator
TOP level function calling NEXT.
GENERATOR resuming execution. Reached end of while loop.
GENERATOR At start of while loop.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment