The following is the first example in “multiprociessing” page of Python doc.
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
I have a project root with directory:
foo
-- __main__.py
-- file.py
Copy paste the above example code into each file.
Running python file.py
or python -m file
works; prints [1,4,9]
.
But running python foo
or python -m foo
doesn’t. And will actually trap you in an “infinite KeyboardInterrupt loop” or something:
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
--- Ctrl + C ---
Process SpawnPoolWorker-8:
Process SpawnPoolWorker-7:
Process SpawnPoolWorker-6:
Process SpawnPoolWorker-5:
Process SpawnPoolWorker-4:
<Some multi-line TraceBack>
KeyboardInterrupt
--- Ctrl + C ---
And you are stuck forever with "Process SpawnPoolWorker-N" where N keeps increasing.
I just want to know why __main__.py
is considered a special case.
From the “Using a pool of workers” section there is a note:
This means that some examples, such as the multiprocessing.pool.Pool examples will not work in the interactive interpreter.
I know that __file__
doesn’t exist in an interactive interpreter:
> python
# enters interpreter
> __file__
NameError: name '__file__' is not defined
but it exists when running a file with python path/to/file.py
So I wondered if __main__.py
is somehow treated as an interpreter, but obviously it’s not and prints the path to the file: path/to/__main__.py
.
Please tell me what’s going on. I mean, if this multiprocessing
package is treating __main__.py
differently, it should say somewhere in the documentation. Or if there is a page that explains how __main__.py
is treated differently which could cause multiprocessing
package to treat it differently and I missed it.
I’ve read a doc page on __main__
but it doesn’t point out any edge case of __main__.py
.
I’m dying to know please help.