Python multiprocessing pool never finishes

Viewed 7551

I am running the following (example) code:

from multiprocessing import Pool

def f(x):
  return x*x

pool = Pool(processes=4)
print pool.map(f, range(10))

However, the code never finishes. What am I doing wrong?

The line

pool = Pool(processes=4)

completes successfully, it appears to stop in the last line. Not even pressing ctrl+c interrupts the execution. I am running the code inside an ipython console in Spyder.

2 Answers
from multiprocessing import Pool


def f(x):
    return x * x


def main():
    pool = Pool(processes=3)  # set the processes max number 3
    result = pool.map(f, range(10))
    pool.close()
    pool.join()
    print(result)
    print('end')


if __name__ == "__main__":
    main()

The key step is to call pool.close() and pool.join() after the processes finished. Otherwise the pool is not released. Besides, you should create the pool in the main process by putting the codes within if __name__ == "__main__":

Related