🐍 Python Intermediate

What is Python's multiprocessing module?

Answer

The multiprocessing module bypasses the GIL by creating separate OS processes with their own Python interpreter and memory space. For CPU-bound parallelism: from multiprocessing import Pool; with Pool(processes=4) as pool: results = pool.map(cpu_task, data_list). Process communication via Queue, Pipe, and shared memory (Value, Array). Synchronization with Lock, Semaphore, Event. Modern approach: concurrent.futures.ProcessPoolExecutor — higher-level and consistent API with ThreadPoolExecutor. Processes have higher startup overhead than threads and serialization cost for passing data between processes (via pickle). Use multiprocessing for CPU-intensive work: image processing, data analysis, scientific computation. For I/O-bound work, prefer asyncio or threads.