How do I use multiprocessing for CPU-bound tasks?

When working with CPU-bound tasks in Python, the multiprocessing module can be highly effective. This allows you to leverage multiple CPU cores to execute tasks in parallel, significantly improving performance for computation-heavy processes.

Here's an example of how to use the multiprocessing module for CPU-bound tasks:

import multiprocessing def worker_function(n): """Function to compute the square of a number.""" return n * n if __name__ == "__main__": # Create a pool of workers with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool: numbers = range(10) results = pool.map(worker_function, numbers) print(results) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Python multiprocessing CPU-bound parallel processing performance optimization