liveBook · Manning

Standard input

As explained in Python Concurrency with asyncio

Standard input is the conventional input stream through which a process receives data from its execution environment or an attached source.

Standard input is a process’s conventional text input stream, commonly connected to a terminal or redirected from another source. In asynchronous Python, ordinary input() blocks the entire thread, preventing background tasks from completing. section 8.4

Use standard input differently depending on the surrounding application:

  • Read directly with input() for simple synchronous command-line programs. section 8.4
  • Adapt sys.stdin to an asyncio StreamReader through connect_read_pipe and StreamReaderProtocol, then await readline(). section 8.4
  • Give an asynchronously created subprocess a PIPE for standard input, then write through its StreamWriter and await drain(). section 13.2

Asynchronous console input

Create a StreamReader, connect it to sys.stdin with StreamReaderProtocol, and await stdin_reader.readline() while delay tasks continue running. This allows input-driven work and background tasks to proceed concurrently. section 8.4

Listing 8.5 An asynchronous standard input reader
import asyncio
from asyncio import StreamReader
import sys
 
 
async def create_stdin_reader() -> StreamReader:
    stream_reader = asyncio.StreamReader()
    protocol = asyncio.StreamReaderProtocol(stream_reader)
    loop = asyncio.get_running_loop()
    await loop.connect_read_pipe(lambda: protocol, sys.stdin)
    return stream_reader

Shows the helper that connects sys.stdin to an asyncio StreamReader. section 8.4

How Python Concurrency with asyncio covers it

How deep it goes
Explains blocking behavior, stream adaptation, subprocess pipes, and concurrent reading and writing, while deferring operating-system internals.
Examples
Uses asynchronous sleep commands, username collection, and interactive echo subprocesses to demonstrate input handling.
What it assumes
Assumes basic Python and asyncio familiarity while establishing how file-like streams, protocols, and subprocess pipes carry input.
What you can do after
Build asynchronous console readers and operate interactive subprocesses using StreamReader, StreamWriter, pipes, and asyncio.
How it connects
Connects standard input with coroutines, streams, protocols, subprocesses, standard output, and deadlock avoidance.

Also explained in

Compare how 5 books explain standard input →

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.