Created
September 16, 2018 01:02
-
-
Save dcoles/e95baeebfa5a843e6c4f4d1d0a437064 to your computer and use it in GitHub Desktop.
Coroutine pipeline
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
""" | |
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