liveBook · Manning

I/O-bound work

As explained in Starting Data Analytics with Generative AI and Python

An I/O-bound task is a computation whose execution time is primarily limited by input or output operations rather than processor computation.

I/O-bound work is limited mainly by input/output operations rather than CPU computation. It spends much of its time waiting for files, networks, or user input. section 7.1.3

Recognize and improve I/O-bound work by:

  • Monitoring CPU usage, disk reads and writes, network activity, and memory while the task runs. section 7.1.3
  • Improving storage speed, optimizing I/O operations, or using asynchronous I/O so other work can continue during waits. section 7.1.3

Typical cases

Reading a large file, downloading internet data, or waiting for user input can be I/O-bound because the program waits on external operations instead of continuously executing CPU instructions. section 7.1.3

def monitor_cpu_io_memory(duration, interval):
    cpu_percentages = []
    io_counters = []
    memory_usages = []

    end_time = time.time() + duration
    while time.time() < end_time:
        cpu_percentages.append(psutil.cpu_percent(interval=interval))
        io_counters.append(psutil.disk_io_counters())
        memory_usages.append(psutil.virtual_memory().used)
        time.sleep(interval)

    return cpu_percentages, io_counters, memory_usages

# Monitor CPU, I/O activity, and memory usage for 10 seconds with 
1-second intervals
cpu_percentages, io_counters, memory_usages = 
monitor_cpu_io_memory(duration=10, interval=1)

# Capture memory usage before running your_function()
memory_before = psutil.virtual_memory().used

def your_function():
    # Your code here
    pass

# Run your function
your_function()

# Capture memory usage after running your_function()
memory_after = psutil.virtual_memory().used

# Calculate function's memory usage
function_memory_usage = memory_after - memory_before

# Analyze CPU usage
avg_cpu_usage = sum(cpu_percentages) / len(cpu_percentages)
print(f"Average CPU usage: {avg_cpu_usage}%")

# Analyze I/O activity
read_bytes = [c.read_bytes for c in io_counters]
write_bytes = [c.write_bytes for c in io_counters]
print(f"Total read bytes: {sum(read_bytes)}")
print(f"Total write bytes: {sum(write_bytes)}")

# Analyze memory usage
avg_memory_usage = sum(memory_usages) / len(memory_usages) - memory_before
print(f"Average memory usage during function execution: 
{avg_memory_usage / (1024 * 1024):.2f} MB")
print(f"Function memory usage: 
{function_memory_usage / (1024 * 1024):.2f} MB")

A Python monitoring workflow samples CPU, disk I/O, and memory over time, then compares memory before and after a function runs. section 7.1.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines the bottleneck, gives examples, recommends measurements, and stops short of detailed asynchronous-I/O implementation.
Examples
Uses large-file reads, internet downloads, user input, and a Python monitor sampling CPU, disk I/O, and memory.
What you can do after
Diagnose/debug with CPU, disk, network, and memory measurements, then select storage, I/O, or asynchronous-I/O improvements.
How it connects
Connects I/O-bound tasks with CPU-bound tasks, resource utilization, asynchronous I/O, storage, network communication, and memory usage.

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 Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.