If it helps, I got reading/writing to stdin/out work like this: output_transport, output_protocol = yield fromloop.connect_write_pipe(BaseProtocol, os.fdopen( > 0, 'wb')) > input_transport, input_protocol = yield from loop.connect_read_pipe( > lambda:OurInputProtocol(session), > sys.stdin)
Not sure whether that's the best way. See the example in https://github.com/jonathanslenders/libpymux and take a look at https://github.com/jonathanslenders/pymux Hope that helps, Jonathan Le mercredi 12 février 2014 11:34:57 UTC+1, Nathan Hoad a écrit : > > Hello, > > On Wednesday, 12 February 2014 20:14:25 UTC+11, Victor Stinner wrote: > > > > Hi, > > > > Tulip now has multiple examples showing how to interact with stdin and > > stdout: > > - examples/child_process.py (low-level API, as you did) > > - examples/shell.py > > - examples/subprocess_shell.py > > Maybe I'm misunderstanding - all of these examples demonstrate > communicating > with stdin/stdout of a subprocess - I am looking to communicate over > stdin/stdout in the current running process, communicating with a parent > process, e.g. a terminal. > > > It looks like you are using an old version, please try the latest > > version (0.3.1). > > Thank you! I've done this now. I feel much better having gotten rid of > that. > > > > > > > It's hard to tell you what is the problem. First, what is the program > > you are running (the one which reads stdin and writes stdout)? > > I am running the given example in a terminal, interacting with it directly > myself. > > > > > > I don't think that _set_nonblocking() is needed. It's done by > > connect_read/write_pipe() if I remember correctly. Where does > > _set_nonblocking() come from? asyncio.unix_events? If yes, please > > don't use private methods. > > Indeed, it would appear that _set_nonblocking is not needed. For the > record, > I know that private methods shouldn't be used - I just thought it would > help > clarify that blocking vs non-blocking wasn't the issue. Sorry for the > confusion. > > > > > > > >> writer = StreamWriter(transport, protocol, reader, EVENT_LOOP) > > > > Here you connect stdin to stdout with the reader parameter. In the > > latest version you can pass None for the reader. But it should not be > > an issue. > > Yep, it doesn't seem to be an issue - I have tried both versions to > completeness. > > > > > If you would like to make sure that all data are written, you may add: > > yield from writer.drain() > > Okay, so calling this meant changing that placeholder Stdio class to > inherit > from asyncio.streams.FlowControlMixin, because the drain method assumes > the > protocol has a _make_drain_waiter method. > > After reading from stdin, I get a ConnectionResetError. > > test > b'test\n' > Traceback (most recent call last): > File "stdio.py", line 31, in <module> > EVENT_LOOP.run_until_complete(stdio()) > File > "/home/nathan/.local/lib/python3.3/site-packages/asyncio/base_events.py", > line 177, in run_until_complete > return future.result() > File > "/home/nathan/.local/lib/python3.3/site-packages/asyncio/futures.py", > line 236, in result > raise self._exception > File "/home/nathan/.local/lib/python3.3/site-packages/asyncio/tasks.py", > line 279, in _step > result = coro.send(value) > File "stdio.py", line 28, in stdio > yield from writer.drain() > File > "/home/nathan/.local/lib/python3.3/site-packages/asyncio/streams.py", > line 247, in drain > raise ConnectionResetError('Connection lost') > ConnectionResetError: Connection lost > > But if I don't read from stdin, then no error occurs: > > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > b'test line\n' > data received test line > > Thank you, > > Nathan. >
