Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 26, 2021 22:36
Show Gist options
  • Save codecakes/109b17f0764ee39fc0db669353711777 to your computer and use it in GitHub Desktop.
Save codecakes/109b17f0764ee39fc0db669353711777 to your computer and use it in GitHub Desktop.
anatomy of consumer producer
import itertools
def p1():
res = None
for x in itertools.cycle(range(5)):
yield x
res = yield
print(f"x={res}")
if res == -1:
print(f"ending term for x={x} res={res}")
yield None
def consumer(*producers):
producers = [each_producer() for each_producer in producers]
for item in itertools.cycle(producers):
print(f"item", item)
if not item:
return
print(next(item))
int_obj = yield
if int_obj:
item.send(int_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment