lang/python/ MultiprocessingVsThreading1
- asyncio is useful for I/O bound tasks, not for CPU bound tasks.
- threading suffers from the Global Interpreter Lock aka GIL preventing more than one thread executing python at the same time (multiple threads can execute C code in parallel).
- multiprocessing allows using separate processes.
Multiprocessing Module
See docs here:
The multiprocessing module also introduces APIs which do not have analogs in the threading module. A prime example of this is the Pool object which offers a convenient means of parallelizing the execution of a function across multiple input values, distributing the input data across processes (data parallelism). The following example demonstrates the common practice of defining such functions in a module so that child processes can successfully import that module. This basic example of data parallelism using Pool,
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))