Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active September 21, 2018 03:45
Show Gist options
  • Select an option

  • Save njsmith/11870e73ddd7b5c86148a0273fcbfff9 to your computer and use it in GitHub Desktop.

Select an option

Save njsmith/11870e73ddd7b5c86148a0273fcbfff9 to your computer and use it in GitHub Desktop.
# 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)
@njsmith
Copy link
Copy Markdown
Author

njsmith commented Sep 21, 2018

Example (lowercase is what I typed, uppercase is printed by the program):

~$ python shouty.py 
hello
HELLO
can you hear me
CAN YOU HEAR ME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment