Skip to content

Instantly share code, notes, and snippets.

@tveastman
Created April 29, 2018 09:45
Show Gist options
  • Save tveastman/fa544fc13cb1d32d03d3c02b1d49f6e3 to your computer and use it in GitHub Desktop.
Save tveastman/fa544fc13cb1d32d03d3c02b1d49f6e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import asyncio
async def get_stream_reader(loop, stream):
reader = asyncio.StreamReader(loop=loop)
reader_protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: reader_protocol, stream)
return reader
async def main(loop):
reader = await get_stream_reader(loop, sys.stdin)
while True:
line = await reader.readline()
if not line:
break
print('read {} from stdin'.format(repr(line)))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment