0

I am trying to execute the following code in jupyter notebook using multiprocessing but the loop is running infinitely.
I need help resolving this issue.

import multiprocessing as mp
import numpy as np

def square(x):
    return np.square(x)

x = np.arange(64)

pool = mp.Pool(4)
squared = pool.map(square, [x[16*i:16*i+16] for i in range(4)])

The output for mp.cpu_count() was 4.

  • Which operating system? If `multiprocessing.get_start_method()` returns "spawn", your code needs to be protected with a `if __name__ == "__main__"` (see Safe importing of main module at https://docs.python.org/3/library/multiprocessing.html – tdelaney Oct 06 '21 at 16:40

2 Answers2

0

You need to rewrite your code to be something like:

def main():
    x = np.arange(64)
    pool = mp.Pool(4)
    squared = .....

if __name__ == '__main__':
    main()

This code is currently being run in every process. You need it to only run in the one process that is doing the setup.

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22
0

You forgot:

pool.close()
pool.join()
Guinther Kovalski
  • 1,629
  • 1
  • 7
  • 15