Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(67)

Unified Diff: examples/tty.py

Issue 66190043: Fix _UnixWritePipeTransport to support TTY
Patch Set: don't set non-blocking mode for TTY Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « asyncio/unix_events.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: examples/tty.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/examples/tty.py
@@ -0,0 +1,33 @@
+"""
+Copy stdin into stdout, line by line.
+"""
+
+import asyncio
+import sys
+from asyncio import streams
+
+class StdoutProtocol(streams.FlowControlMixin,
+ asyncio.Protocol):
+ pass
+
+@asyncio.coroutine
+def copy_stdin_to_stdout(limit):
+ reader = asyncio.StreamReader(limit=limit)
+ reader_protocol = asyncio.StreamReaderProtocol(reader)
+ yield from loop.connect_read_pipe(lambda: reader_protocol,
+ sys.stdin)
+ transport, protocol = yield from loop.connect_write_pipe(StdoutProtocol,
+ sys.stdout)
+ writer = streams.StreamWriter(transport, protocol, reader, loop)
+ while True:
+ line = yield from reader.readline()
+ if not line:
+ break
+ writer.write(line)
+ yield from writer.drain()
+
+# readline() fails with ValueError('Line is too long') if a longer line is
+# received
+max_line_len = 4096
+loop = asyncio.get_event_loop()
+loop.run_until_complete(copy_stdin_to_stdout(max_line_len))
« no previous file with comments | « asyncio/unix_events.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b