What types of concurrency are suitable for optimizing the execution of CPU-intensive tasks?
sobes.tech AI
Answer from AI
For tasks requiring intensive CPU computations, the optimal types of concurrency in Python are:
-
Multiprocessing: since the Global Interpreter Lock (GIL) limits the concurrent execution of threads in Python, using multiple processes allows for effective parallelization of computations across multiple CPU cores.
-
Using libraries with native code: for example, NumPy, which implements computations in C and releases the GIL.
-
Parallel execution using concurrent.futures.ProcessPoolExecutor — a convenient interface for running tasks in separate processes.
An example of using multiprocessing to compute factorials in multiple processes:
from multiprocessing import Pool
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
if __name__ == '__main__':
numbers = [5, 7, 10, 12]
with Pool() as pool:
results = pool.map(factorial, numbers)
print(results)
Thus, for CPU-intensive tasks, it is better to use multiprocessing rather than multithreading.