Skip to content

Instantly share code, notes, and snippets.

@dcoles
Created September 16, 2018 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcoles/e95baeebfa5a843e6c4f4d1d0a437064 to your computer and use it in GitHub Desktop.
Save dcoles/e95baeebfa5a843e6c4f4d1d0a437064 to your computer and use it in GitHub Desktop.
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