Skip to content

Instantly share code, notes, and snippets.

@matze
Created September 18, 2013 09:07
Show Gist options
  • Save matze/6606560 to your computer and use it in GitHub Desktop.
Save matze/6606560 to your computer and use it in GitHub Desktop.
Co-routine example with multicast
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