[Python-ideas] Hooks into the IO system to intercept raw file reads/writes

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Feb 4 09:32:46 CET 2015


Paul Moore wrote:
> I found the asyncio docs a bit of a struggle, TBH. Is
> there a tutorial? The basic idea is something along the lines of
> https://docs.python.org/3.4/library/asyncio-subprocess.html#subprocess-using-streams
> but I don't see how I'd modify that code (specifically the "yield from
> proc.stdout.readline()" bit) to get output from either stdout or
> stderr, whichever was ready first (and handle the 2 cases
> differently).

One way is to use two subsidiary coroutines, one
for each pipe.

The following seems to work:

#-----------------------------------------------------

import asyncio.subprocess
import sys

@asyncio.coroutine
def handle_pipe(label, pipe):
     while 1:
         data = yield from pipe.readline()
         if not data:
             return
         line = data.decode('ascii').rstrip()
         print("%s: %s" % (label, line))

@asyncio.coroutine
def run_subprocess():
     cmd = 'echo foo ; echo blarg >&2'
     proc = yield from asyncio.create_subprocess_exec(
         '/bin/sh', '-c', cmd,
             stdout = asyncio.subprocess.PIPE,
             stderr = asyncio.subprocess.PIPE)
     h1 = asyncio.async(handle_pipe("STDOUT", proc.stdout))
     h2 = asyncio.async(handle_pipe("STDERR", proc.stderr))
     yield from asyncio.wait([h1, h2])

if sys.platform == "win32":
     loop = asyncio.ProactorEventLoop()
     asyncio.set_event_loop(loop)
else:
     loop = asyncio.get_event_loop()

loop.run_until_complete(run_subprocess())

#-----------------------------------------------------

% python3.4 asyncio_subprocess.py
STDOUT: foo
STDERR: blarg

-- 
Greg


More information about the Python-ideas mailing list