New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Very first multiprocessing example not working on Windows 11 #90072
Comments
Very first multiprocessing example not working on Windows 11 https://docs.python.org/3/library/multiprocessing.html 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])) Tried Python 3.7, 3.8, 3.9, 3.10 >>> from multiprocessing import Pool
>>> def f(x):
... return x*x
...
>>> with Pool(5) as p:
... print(p.map(f, [1, 2, 3]))
...
Process SpawnPoolWorker-14:
Process SpawnPoolWorker-13:
Traceback (most recent call last):
Process SpawnPoolWorker-15:
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Program Files\Python39\lib\multiprocessing\pool.py", line 114, in worker
task = get()
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python39\lib\multiprocessing\queues.py", line 368, in get
return _ForkingPickler.loads(res)
File "C:\Program Files\Python39\lib\multiprocessing\pool.py", line 114, in worker
task = get()
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
File "C:\Program Files\Python39\lib\multiprocessing\queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python39\lib\multiprocessing\pool.py", line 114, in worker
task = get()
File "C:\Program Files\Python39\lib\multiprocessing\queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)> |
The Windows API only supports the spawn method of process creation. In POSIX (except macOS), the default is the fork method, for which the child inherits the interactive main module of the parent. If you switch to the spawn method in Linux via multiprocessing.set_start_method('spawn'), you'll see the same error. multiprocessing is one package where it's necessary in Windows to test examples using a script. This is implied in the guidelines when it says to "[m]ake sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process)". It's the fact the main module has to be importable by child processes that matters in this case. The behavior is noted with an example at the end of the introduction:
If it were up to me this note would be at the beginning of the introduction, where everyone would certainly see it. As is, the reader is expected to at least scan over the entire introduction. |
I think something should be said right after the example. But the 20 line note seems a bit too much at this location. Perhaps insert (For reasons given _below_, this will raise error if enter interactively.) with _below_, say, linked to the 20 line note. It is not quite clear to me it this only applies on Windows or is general. |
macOS defaults to the spawn method (bpo-33725). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: