In this lesson, you will learn how to profile your application to determine if your tasks are compute-heavy enough to warrant the overhead of process-based parallelism.
The Anatomy of CPU-Bound Workloads
A CPU-bound workload is one where the program’s execution speed is limited by the processor’s ability to perform calculations, rather than by waiting for external events. If you find your application spending 99% of its time performing mathematical transformations, image processing, data encryption, or heavy serialization, you are dealing with a CPU-bound task.
Because of the Global Interpreter Lock (GIL), Python threads cannot execute bytecode in parallel on multiple CPU cores. If a task is purely computational, threading will offer zero performance gains—in fact, it often slows down the application due to the overhead of context switching.
Identifying the Bottleneck
To determine if your task requires multiprocessing instead of threading, look at your CPU utilization during execution. If one core is pinned at 100% while others remain idle, you have identified a single-threaded CPU bottleneck.
Consider the following example:
import time
def heavy_computation(n):
# This task is purely CPU-bound (e.g., calculating prime numbers)
return sum(i * i for i in range(n))
# If this takes 10 seconds and keeps one core at 100% usage,
# it is a prime candidate for process-based parallelism.
start = time.time()
heavy_computation(10**7)
print(f"Execution took {time.time() - start:.2f} seconds")
If you attempt to speed this up using the threading module, the GIL will force the threads to take turns. You will see no reduction in time. However, by offloading this to a separate process, the operating system can schedule these tasks across multiple physical cores, bypassing the GIL entirely.
When to Switch
Only shift to process-based parallelism when:
- The task is computationally expensive: The work performed per byte of data is high.
- The overhead is justified: Since creating processes is more expensive than creating threads, ensure the computation time significantly exceeds the time it takes to spawn the process and communicate data via Inter-Process Communication (IPC).
Key Takeaway: If your bottleneck is the processor's calculation speed, ignore threading and utilize multiprocessing to distribute the workload across multiple physical CPU cores.