E
E
Eva052017-05-16 16:25:03
Python
Eva05, 2017-05-16 16:25:03

How to make the multiprocessing module work on IPython?

Good day!
I work in IPython, there is a code

from multiprocessing import Pool
import multiprocessing
def job(num):
    return num * 2
if __name__=='__main__':
    p=Pool(processes=20)
    data=p.map(job,range(20))
    p.close()
    print(data)

Instead of displaying values ​​multiplied by two in the interactive environment, the window simply freezes and does nothing.
It doesn't even report any errors.
The examples from the official documentation also do not work and sag
Tell me what the problem is and how to make the code still execute the function and display the values ​​on the screen.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Karelin, 2017-05-28
@Eva05

Unfortunately, your question is not quite complete...
What does "I work in IPython" mean? Is it a notebook, or a console, which one? What versions of all software (Python, IPython / Jupyter)? Operating system?
I tried to run your example in the Qt console, and at the same time I had the system console window open (DOS Prompt on Windows 8), and a long stacktrace fell out into the system window with the following ending:

File "C:\Anaconda3\lib\multiprocessing\queues.py", line 345, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'job' on <module '__main__' (built-in)>

and indeed, IPython hung up.
And the kernel restart does not work. I also advise you to explore and read how to use Pool. And maybe there is some bug in IPython.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question