Created
September 18, 2013 09:07
-
-
Save matze/6606560 to your computer and use it in GitHub Desktop.
Co-routine example with multicast
This file contains 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
def coroutine(func): | |
def start(*args, **kwargs): | |
g = func(*args, **kwargs) | |
g.next() | |
return g | |
return start | |
def produce(consumer): | |
for item in (1,2,3,4): | |
consumer.send(item) | |
@coroutine | |
def multicast(*destinations): | |
while True: | |
item = yield | |
for destination in destinations: | |
destination.send(item) | |
@coroutine | |
def write(fname): | |
while True: | |
item = yield | |
print("Write {} to {}".format(item, fname)) | |
@coroutine | |
def display(): | |
while True: | |
item = yield | |
print("Display {}".format(item)) | |
produce(multicast(write('/dev/null'), display())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment