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




