Junior — Middle
What methods are used to perform parallel task execution on multiprocessor systems?
sobes.tech AI
Answer from AI
Parallel execution of tasks on multi-processor systems in Python is implemented in several ways:
-
multiprocessing module — allows creating processes that run in parallel and are not limited by GIL (Global Interpreter Lock). This is an efficient method for CPU-bound tasks.
-
Threads (threading) — suitable for I/O-bound tasks, as in Python, due to GIL, threads do not execute in parallel on multiple cores.
-
concurrent.futures module — provides a convenient interface for running tasks in threads (
ThreadPoolExecutor) or processes (ProcessPoolExecutor).
Example of using multiprocessing:
from multiprocessing import Pool
def f(x):
return x * x
if __name__ == '__main__':
with Pool(4) as p:
results = p.map(f, [1, 2, 3, 4, 5])
print(results) # [1, 4, 9, 16, 25]