Why are you posting code that uses an internal API (stdout._fileno) and which you know is broken? There's no way you can get this to work by intercepting the FD of a stream. There's no way you can fix the "race condition".
I'm confident it can be done using the lower-level API (event loop methods). You will have to call os.pipe() yourself and pass one end to the first process, another to the second. You may use some of the helper classes from asyncio/subprocess.py to tie the outer ends to streams. (Maybe those classes need to be made officially public.) But why not use shell syntax using '|'? On Wed, Feb 19, 2014 at 7:53 AM, Victor Stinner <[email protected]>wrote: > Hi, > > It looks like it's not possible to connect the stdout of a subprocess > to the stdin of another subprocess using the API of the > asyncio.subprocess module. > > IMO the (subprocess.)Popen objects should be created first, pipes > connected, and then create (asyncio.subprocess.)Process wrapper. Or > maybe a new API is needed. > > My test: > --- > import asyncio > import asyncio.subprocess > > @asyncio.coroutine > def task(): > cat = yield from asyncio.create_subprocess_shell( > 'cat /etc/passwd', > stdout=asyncio.subprocess.PIPE) > stdout = cat._transport.get_pipe_transport(1) > # yield from asyncio.sleep(1) > grep = yield from asyncio.create_subprocess_shell( > 'grep haypo', > stdin=stdout._fileno, > stdout=asyncio.subprocess.PIPE) > stdout.close() > stdout, stderr = yield from grep.communicate() > print("stdout >>>%r<<<" % stdout) > > loop = asyncio.get_event_loop() > loop.run_until_complete(task()) > --- > > This example is a major race condition: if cat produces data before > the pipe is connected to grep stdin, data is lost (not passed to grep, > but written into cat.stdout stream reader). > > Victor > -- --Guido van Rossum (python.org/~guido)
