Junior — Middle
Have you used the multiprocessing library for parallel data processing in Python?
sobes.tech AI
Answer from AI
Yes, the multiprocessing library in Python allows creating processes for parallel execution, which is especially useful for CPU-intensive tasks, bypassing the GIL (Global Interpreter Lock).
Main features:
- Creating new processes with
Process - Using queues (
Queue) and pipes (Pipe) for data exchange between processes - Process pools (
Pool) for task distribution
Example of using multiprocessing.Pool for parallel processing of a list of numbers:
from multiprocessing import Pool
def square(x):
return x * x
if __name__ == '__main__':
with Pool(4) as p:
results = p.map(square, [1, 2, 3, 4, 5])
print(results) # [1, 4, 9, 16, 25]
This approach speeds up processing large data volumes by utilizing multiple CPU cores.