liveBook · Manning

Separation of concerns

As explained in Python Concurrency with asyncio

Separation of concerns is a design technique that gives distinct responsibilities to separate components or computations.

Separation of concerns assigns distinct responsibilities to separate components or computations. In asynchronous subprocess communication, coupling output reading with input writing becomes difficult to follow as I/O grows more complex. section 13.2

Coordinating an interactive subprocess

An output-consumer coroutine reads standard output until it sees the expected prompt, then sets an Event. An input-writer coroutine waits for that event, writes input, drains the stream, and clears the event. Running both coroutines with gather gives each one clear responsibility. section 13.2

Listing 13.9 Echoing user input
username = input('Please enter a username: ')
print(f'Your username is {username}')

Inspect the coroutine-based implementation that separates prompt detection from input writing. section 13.2

How Python Concurrency with asyncio covers it

How deep it goes
Defines the split, assigns one responsibility to each coroutine, coordinates them with an event, and runs them concurrently with gather.
Examples
Uses interactive echo programs, including randomized repeated output and prompt-driven input, to expose coupling and motivate separate coroutines.
What you can do after
Apply it via asyncio by coordinating output consumption and input writing with an Event and gather.
How it connects
Connects coroutines, subprocesses, standard input, standard output, events, StreamReader, StreamWriter, and concurrency.

Also explained in

Compare how 22 books explain separation of concerns →

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.