Created
October 26, 2021 22:36
-
-
Save codecakes/109b17f0764ee39fc0db669353711777 to your computer and use it in GitHub Desktop.
anatomy of consumer producer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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