Threads for I/O-Bound Versus CPU-Bound Work
import threading, time
def cpu_task():
total = 0
for i in range(50_000_000):
total += i
def io_task():
time.sleep(1)
Scenario 1: start four threads, each running cpu_task(). Scenario 2: start four threads, each running io_task(). Compare each scenario's wall-clock time against running the same four calls one after another in a single thread. In CPython, which scenario sees a real, meaningful speedup from using threads?
Sign in to answer questions and track your progress
Sign In