liveBook · Manning

Process.stdout

As explained in Python Concurrency with asyncio

Process.stdout is the Process attribute that exposes a subprocess's redirected standard-output stream reader.

Process.stdout exposes a redirected subprocess's standard-output stream as a StreamReader. Set stdout to asyncio.subprocess.PIPE when creating the process, then read the stream asynchronously, often line by line. section 13.1.1

Prefixing subprocess output

A write_output coroutine can read Process.stdout with readline(), decode each line, and prepend the originating command before printing it. Run that task alongside process.wait() to report output and completion without confusion. section 13.1.1

Listing 13.3 Using the standard output stream reader
import asyncio
from asyncio import StreamReader
from asyncio.subprocess import Process
 
 
async def write_output(prefix: str, stdout: StreamReader):
    while line := await stdout.readline():
        print(f'[{prefix}]: {line.rstrip().decode()}')
 
 
async def main():
    program = ['ls', '-la']
    process: Process = await asyncio.create_subprocess_exec(*program,
                                                            stdout=asyncio
                                                            .subprocess.PIPE)
    print(f'Process pid is: {process.pid}')
    stdout_task = asyncio.create_task(write_output(' '.join(program), process.stdout))
 
    return_code, _ = await asyncio.gather(process.wait(), stdout_task)
    print(f'Process returned: {return_code}')
 
 
asyncio.run(main())

Shows Process.stdout consumed by a StreamReader task while an asyncio subprocess runs, including the returned process code. section 13.1.1

Always consume substantial piped output. Waiting without reading can deadlock when the pipe fills; process.communicate() provides a simpler alternative for collecting stdout and stderr. section 13.1.1

How Python Concurrency with asyncio covers it

How deep it goes
Defines the StreamReader interface, demonstrates line-by-line consumption, and warns that unread pipes can deadlock process.wait().
Examples
Uses ls -la output, prefixed command lines, and a million-line producer to demonstrate normal reading and deadlock.
What you can do after
Operate redirected subprocess output safely by consuming Process.stdout or using communicate() for collection.
How it connects
Connects subprocess creation, asyncio tasks, StreamReader, pipes, process.wait(), and communicate().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.