liveBook · Manning

I/O

As explained in Python Concurrency with asyncio

An I/O operation transfers data between a program and an external resource such as a file, device, or network endpoint.

I/O transfers data between a program and an external resource, such as a keyboard, hard drive, or network card. Because these operations may wait for input or a web response, synchronous code can block the application and make it unresponsive. section 1.1

Concurrency lets an application handle multiple I/O operations together, such as simultaneous web requests. asyncio provides an asynchronous programming model for this, while threads can also overlap I/O because Python releases the GIL during those operations. section 1.1 section 1.5.1

Reading two web responses

Reading example.com twice synchronously takes about 0.2306 seconds in the demonstrated run. Starting one thread per request reduces the example to about 0.0977 seconds, although network conditions affect results. section 1.5.1

Listing 1.7 Synchronously reading status codes
import time
import requests
 
 
def read_example() -> None:
    response = requests.get('https:/ / www .example .com')
    print(response.status_code)
 
 
sync_start = time.time()
 
read_example()
read_example()
 
sync_end = time.time()
 
print(f'Running synchronously took {sync_end - sync_start:.4f} seconds.')

Compare the synchronous status-code example with its multithreaded counterpart to see how waiting on network I/O affects elapsed time. section 1.5.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains blocking, concurrency, and GIL release during I/O, but stops short of detailed system-call mechanics.
Examples
Uses keyboards, hard drives, network cards, web APIs, and repeated example.com status-code requests.
What you can do after
Apply concurrency with asyncio or threads to overlap waiting web requests and improve responsiveness.
How it connects
Connects I/O with concurrency, asynchronous programming, asyncio, threads, the GIL, and parallelism.

Also explained in

Compare how 4 books explain I/O →

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.