23

I am running the following code in iPython:

import multiprocessing

def my_function(x):
    """The function you want to compute in parallel."""
    x += 1
    return x


if __name__ == '__main__':
    pool = multiprocessing.Pool()
    results = pool.map(my_function, [1,2,3,4,5,6])
    print(results)

in ipython QT console on Windows. However, the code does not work -- the QT console just freezes up. The issue is specific to iPython (the code above should work for the regular Python 2.7).

Any solution to this?

karlson
  • 5,325
  • 3
  • 30
  • 62
user3438258
  • 237
  • 1
  • 3
  • 9

2 Answers2

21

From the documentation:

Note

Functionality within this package requires that the __main__ module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter

dano
  • 91,354
  • 19
  • 222
  • 219
16

At least in the current version of Jupyter Notebook (the successor of IPython) you can solve this by moving the target function to a separate module, and importing it.

I have no idea why this works, it's rather odd, but it does.

i.e. - in a workers.py file put

def my_function(x):
    """The function you want to compute in parallel."""
    x += 1
    return x

Then in IPython/Jupyter notebook put:

import multiprocessing
import workers

pool = multiprocessing.Pool()
results = pool.map(workers.my_function, [1,2,3,4,5,6])
print(results)

also - the if-main thingy doesn't seem to be needed.

Credit: Gaurav Singhal

Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
  • This works but it seems like the performance has no increase because it looks like still running in a single process/thread. – John Zhang May 26 '20 at 12:07
  • @JohnZhang I get different process id's when I run this (although sometimes I do get the same process). You can just add os.getpid() to the worker.py (make sure to import os). – Maverick Meerkat May 26 '20 at 16:28
  • In my test, I will get MP if I run it in Python console. However, if I run it in Jupyter, it will be always 1 process. I think it makes sense since one Jupyter notebook is one process in Jupyter architecture (at least on Windows). – John Zhang May 27 '20 at 04:43
  • 2
    @JohnZhang have you used pool? I also use windows and I do get multiprocessing. Also it doesn't make sense, as every IDE or even console you use is one process, and they all spawn different processes. – Maverick Meerkat May 27 '20 at 07:54