Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the Python module Multiprocessing

Comments

  1. multiprocess is a fork of the Python standard libary multiprocessing . multiprocess extends multiprocessing to provide enhanced serialization, using dill. multiprocess leverages multiprocessing to support the spawning of processes using the API of the python standard library's threading module.

  2. multiprocessing.Pool.map does not work with lambda functions due to the fact that lambda functions cannot be pickled. There are multiple approaches to avoid the issue. You can define a function or use functools.partial.

In [10]:
from multiprocessing import Pool
In [11]:
pool = Pool(2)
In [12]:
numbers = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16],
]
In [13]:
pool.map(sum, numbers)
Out[13]:
[10, 26, 42, 58]
In [ ]:
 

Comments