liveBook · Manning

I/O-bound work

As explained in Python Concurrency with asyncio

Blocking I/O work is work whose progress can wait for input or output operations that do not complete immediately.

I/O-bound work spends most of its time waiting for a network or other input/output device. Its limiting factor is I/O capacity: faster bandwidth, network hardware, or storage can reduce completion time. CPU-bound work, by contrast, is limited primarily by computation. Real applications often combine both, such as downloading a response, formatting its headers, and writing them to a file. section 1.2

Asynchronous I/O pauses a task while its I/O operation waits, allowing other code to run. In asyncio, an event loop schedules tasks, pauses those waiting for I/O, and wakes them when the operating system reports completion. This enables many I/O operations to progress concurrently in one process and thread. section 1.2 section 1.7

Choose the concurrency approach according to what limits the work:

  • Use asyncio and non-blocking I/O when many independent operations spend time waiting for network or device responses. section 1.0 section 1.8
  • Use threads to run existing blocking I/O APIs concurrently; thread pools can combine blocking calls with asyncio coordination. section 7.6
  • Do not expect asyncio or ordinary Python threads to make CPU-bound code parallel; CPU-intensive work generally calls for processes. section 1.5.2 section 1.4.2

A mixed I/O and CPU pipeline

A program requests a web page, formats the response headers in Python, then writes the result to headers.txt. The request and file write are I/O-bound; the header formatting is CPU-bound. With asynchronous I/O, several requests can wait concurrently instead of blocking one another. section 1.2

Listing 1.1 I/O-bound and CPU-bound operations
import requests
 
response = requests.get('https:/ / www .example .com')     #1
 
items = response.headers.items()
 
headers = [f'{key}: {header}' for key, header in items]    #2
 
formatted_headers = '\n'.join(headers)                     #3
 
with open('headers.txt', 'w') as file:
    file.write(formatted_headers)                          #4

Inspect the complete Python pipeline showing a web request, CPU-bound header processing, and file output. section 1.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains limiting factors, mixed workloads, asynchronous pausing, and event-loop scheduling without detailing device internals.
Examples
Uses web requests, file reads and writes, database queries, header formatting, and concurrent product lookups as concrete examples.
What you can do after
Apply it with asyncio, non-blocking sockets, connection pools, or thread pools to run independent blocking operations concurrently.
How it connects
Connects I/O-bound work to CPU-bound work, asynchronous I/O, non-blocking I/O, event loops, threads, processes, and the GIL.

Also explained in

Compare how 5 books explain I/O-bound work →

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.