If I run this simple code in IDLE in Python 2.7.8, it will pop a window saying "The program is still running! Do you want to kill it?".
from multiprocessing import Pool
def foo(x):
return x**2
if __name__ == '__main__':
pool = Pool(2)
pows = pool.map(foo, range(10))
print pows
Even if I do kill or not (it will ask twice) nothing will happen. I used to use Windows and I've just recently started using Mac OSX (10.9.4), and I don't know if I'm missing something here.
If I run the same code directly in the Python Shell in terminal, it will run fine. Same in iPython notebook. It just won't on IDLE, popping up that message box.
Any ideas? I'd like to keep using IDLE...
here's the log:
INFO:root:10221: Started process
INFO:root:10221: Defined foo
INFO:root:10221: __name__ == '__main__'
INFO:root:10221: pool created
This was a known issue with the previous version of Pycharm. If you upgrade with the latest version now you can safely use multiprocessing within the console of the IDE without running in this issue any longer.
See here for further informations: https://youtrack.jetbrains.com/issue/PY-14969
Ref this:
https://docs.python.org/2/library/multiprocessing.html#introduction
Specifically, in the 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."
Here's a similar question Child processes created with python multiprocessing module won't print
Example of logging activity to a file:
#!/usr/bin/env python
import logging
from multiprocessing import Pool
import os
logging.basicConfig(filename='example.log',level=logging.DEBUG)
def log_msg(msg):
logging.info("{}: {}".format(os.getpid(), msg))
log_msg("Started process")
def foo(x):
log_msg("running foo")
return x**2
log_msg("Defined foo")
if __name__ == '__main__':
log_msg("__name__ == '__main__'")
pool = Pool(2)
log_msg("pool created")
pows = pool.map(foo, range(10))
log_msg("map completed")
print pows
log_msg("output printed")
log_msg("Finished running")
Example output for me:
tom@fannybawz:~$ ./multiproc.py
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
tom@fannybawz:~$ cat example.log
INFO:root:22238: Started process
INFO:root:22238: Defined foo
INFO:root:22238: __name__ == '__main__'
INFO:root:22238: pool created
INFO:root:22240: {}: running foo
INFO:root:22239: {}: running foo
INFO:root:22240: {}: running foo
INFO:root:22239: {}: running foo
INFO:root:22240: {}: running foo
INFO:root:22239: {}: running foo
INFO:root:22240: {}: running foo
INFO:root:22239: {}: running foo
INFO:root:22240: {}: running foo
INFO:root:22240: {}: running foo
INFO:root:22238: map completed
INFO:root:22238: output printed
INFO:root:22238: Finished running
tom@fannybawz:~$
Try the same thing yourself with the Process version.