Last active
September 21, 2018 03:45
-
-
Save njsmith/11870e73ddd7b5c86148a0273fcbfff9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Trio program that reads from stdin, uppercases everything, and then echoes | |
| # it back to stdout. | |
| # This uses some internal APIs, because Trio v0.7.0 doesn't have a real public | |
| # API for talking to stdin/stdout yet. This only works on Unix. | |
| # If you want to help implement a real API for this, or just check whether | |
| # anything new has happened, see: | |
| # https://github.com/python-trio/trio/issues/174 | |
| import trio | |
| # Temporary hack until Trio has a real API for this: | |
| from trio._subprocess.unix_pipes import PipeSendStream, PipeReceiveStream | |
| async def main(): | |
| stdin = PipeReceiveStream(0) | |
| stdout = PipeSendStream(1) | |
| stream = trio.StapledStream(stdout, stdin) | |
| while True: | |
| data = await stream.receive_some(9999) # arbitrary buffer size | |
| if not data: | |
| break | |
| await stream.send_all(data.upper()) | |
| trio.run(main) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example (lowercase is what I typed, uppercase is printed by the program):