Comparing threads with processes requires choosing CPU- or I/O-bound work, including startup overhead and measuring identical outputs.
Understanding the Concept
Comparing threads with processes requires choosing CPU- or I/O-bound work, including startup overhead and measuring identical outputs. The right choice changes with workload size and platform.
Do not assume one model always wins; verify correctness and include CPU and elapsed time.
How the event loop makes progress
When a coroutine reaches await, it may suspend so another ready Task can execute. This scheduling is cooperative: blocking operations that do not yield hold up every coroutine sharing the event loop. Assign ownership of background tasks, put timeouts around operations that may wait, and release resources on cancellation.
An async function does not automatically use several CPU cores. Use async-compatible I/O for waiting tasks and executors or separate processes for blocking or CPU-heavy work when appropriate.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
from time import perf_counter
def square(number):
return number * number
start = perf_counter()
values = [square(number) for number in range(2000)]
print(len(values), perf_counter() - start >= 0)
# Benchmark guarded executors separately in saved script files.
Walkthrough: What Each Part Does
- The imports make time available. These are standard-library building blocks unless the example explicitly names an external tool.
- The definition square creates reusable behavior. Its body runs when called, awaited or instantiated; defining it alone does not run the body of an ordinary function.
- The top-level entry point starts event-loop execution and prints a result after the awaited operation is complete.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
Example Output
2000 True
The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.
Important Behavior and Edge Cases
Timing two models with different data sizes or ignoring process startup and serialization costs.
Do not assume one model always wins; verify correctness and include CPU and elapsed time.
Practice Exercise
Benchmark a saved script using ThreadPoolExecutor and ProcessPoolExecutor on increasing workloads.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Timing two models with different data sizes or ignoring process startup and serialization costs.
- Calling a blocking function inside the event loop, or creating tasks without awaiting, cancelling or owning them.
Key Takeaways
- Comparing threads with processes requires choosing CPU- or I/O-bound work, including startup overhead and measuring identical outputs.
- Do not assume one model always wins; verify correctness and include CPU and elapsed time.
- Practice: Benchmark a saved script using ThreadPoolExecutor and ProcessPoolExecutor on increasing workloads.