Junior — Middle
Is it possible to implement multiprocessing by distributing it across different processor cores?
sobes.tech AI
Answer from AI
Yes, Python can implement multiprocessing by distributing tasks across different CPU cores. The multiprocessing module creates separate processes, each capable of running on a different core, bypassing the GIL (Global Interpreter Lock).
Example of using multiprocessing:
from multiprocessing import Pool
def f(x):
return x * x
if __name__ == '__main__':
with Pool(4) as p: # create a pool of 4 processes
results = p.map(f, [1, 2, 3, 4, 5])
print(results) # [1, 4, 9, 16, 25]
Thus, tasks are distributed among processes that can run in parallel on different cores.