Sometimes, we want to parallelize a simple Python loop.
In this article, we’ll look at how to parallelize a simple Python loop.
How to parallelize a simple Python loop?
To parallelize a simple Python loop, we can use the joblib
module.
For instance, we write:
from joblib import Parallel, delayed
def process(i):
return i * i
results = Parallel(n_jobs=2)(delayed(process)(i) for i in range(10))
print(results)
We define our loop header with for i in range(10)
.
And in the loop body, we run delayed(process)(i)
.
Finally, we use the Parallel
constructor with n_jobs
set to 2 to use 2 CPU cores.
And then we assign the return results to results
.
Therefore, results
is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
.
Conclusion
To parallelize a simple Python loop, we can use the joblib
module.