multiprocessing pool example does not work and freeze the kernel

This happens because you didn’t protect your “procedural” part of the code from re-execution when your child processes are importing f.

They need to import f, because Windows doesn’t support forking as start method for new processes (only spawn). A new Python process has to be started from scratch, f imported and this import will also trigger another Pool to be created in all child-processes … and their child-processes and their child-processes…

To prevent this recursion, you have to insert an if __name__ == '__main__': -line between the upper part, which should run on imports and a lower part, which should only run when your script is executed as the main script (only the case for the parent).

from multiprocessing import Pool

def f(x):
  return x*x

if __name__ == '__main__': # protect your program's entry point

    p = Pool(6)
    print(p.map(f, range(10))) 

Separating your code like that is mandatory for multiprocessing on Windows and Unix-y systems when used with ‘spawn’ or ‘forkserver’ start-method instead of default ‘fork’. In general, start-methods can be modified with multiprocessing.set_start_method(method).

Since Python 3.8, macOS also uses ‘spawn’ instead of ‘fork’ as default.

It’s general a good practice to separate any script in upper “definition” and lower “execution as main”, to make code importable without unnecessary executions of parts only relevant when run as top level script. Last but not least, it facilitates understanding the control flow of your program when you don’t intermix definitions and executions.

Leave a Comment