Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coroutine pipeline
"""
Coroutine pipeline.
Based off https://www.lua.org/pil/9.2.html.
"""
import types
if __name__ == '__main__':
def receive(prod):
return prod.send(None)
@types.coroutine
def send(x):
yield x
async def producer():
while True:
x = input("? ")
await send(x)
def filter(prod):
async def _filter():
line = 1
while True:
x = receive(prod)
await send("%5d %s" % (line, x))
line = line + 1
return _filter()
def consumer(prod):
while True:
x = receive(prod)
print(x)
# Simple pipeline
consumer(filter(producer()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment