Mini Project (Parallel Number Processing)
Goal
Create a CPU-bound job and speed it up using a process pool.
Example job:
- compute something expensive for many items
Step 1: Define an expensive function
expensive.py
def expensive(x: int) -> int:
# intentionally slow operation
total = 0
for i in range(1, 200_000):
total += (x * i) % 97
return totalexpensive.py
def expensive(x: int) -> int:
# intentionally slow operation
total = 0
for i in range(1, 200_000):
total += (x * i) % 97
return totalStep 2: Run sequential
sequential.py
import time
from expensive import expensive
items = list(range(20))
start = time.time()
results = [expensive(x) for x in items]
print("sequential seconds:", round(time.time() - start, 2))
print(results[:5])sequential.py
import time
from expensive import expensive
items = list(range(20))
start = time.time()
results = [expensive(x) for x in items]
print("sequential seconds:", round(time.time() - start, 2))
print(results[:5])Step 3: Run in parallel (Pool)
parallel.py
import time
from multiprocessing import Pool
from expensive import expensive
items = list(range(20))
if __name__ == "__main__":
start = time.time()
with Pool() as pool:
results = pool.map(expensive, items)
print("parallel seconds:", round(time.time() - start, 2))
print(results[:5])parallel.py
import time
from multiprocessing import Pool
from expensive import expensive
items = list(range(20))
if __name__ == "__main__":
start = time.time()
with Pool() as pool:
results = pool.map(expensive, items)
print("parallel seconds:", round(time.time() - start, 2))
print(results[:5])Deliverable
- Compare sequential vs parallel time.
- Try different pool sizes.
- Explain when multiprocessing helps (CPU-bound) vs not (I/O-bound).
๐งช Try It Yourself
Exercise 1 โ Start a Process
Exercise 2 โ Process Pool map()
Exercise 3 โ Multiprocessing Queue
If this helped you, consider buying me a coffee โ
Buy me a coffeeWas this page helpful?
Let us know how we did
