Skip to content
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

Open
quattrozhou mannequin opened this issue Nov 28, 2021 · 4 comments
Open

Very first multiprocessing example not working on Windows 11 #90072

quattrozhou mannequin opened this issue Nov 28, 2021 · 4 comments
Labels
3.11 only security fixes docs Documentation in the Doc dir OS-windows topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@quattrozhou
Copy link
Mannequin

quattrozhou mannequin commented Nov 28, 2021

BPO 45914
Nosy @rhettinger, @terryjreedy, @pfmoore, @tjguk, @zware, @eryksun, @zooba, @quattrozhou

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:

assignee = None
closed_at = None
created_at = <Date 2021-11-28.04:37:54.305>
labels = ['3.11', 'type-bug', 'OS-windows', 'docs']
title = 'Very first multiprocessing example not working on Windows 11'
updated_at = <Date 2021-12-04.07:37:31.190>
user = 'https://github.com/quattrozhou'

bugs.python.org fields:

activity = <Date 2021-12-04.07:37:31.190>
actor = 'eryksun'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'Windows']
creation = <Date 2021-11-28.04:37:54.305>
creator = 'quattrozhou'
dependencies = []
files = []
hgrepos = []
issue_num = 45914
keywords = []
message_count = 4.0
messages = ['407186', '407189', '407638', '407646']
nosy_count = 9.0
nosy_names = ['rhettinger', 'terry.reedy', 'paul.moore', 'tim.golden', 'docs@python', 'zach.ware', 'eryksun', 'steve.dower', 'quattrozhou']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45914'
versions = ['Python 3.11']

@quattrozhou
Copy link
Mannequin Author

quattrozhou mannequin commented Nov 28, 2021

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
Also tried Ubuntu which works fine.
But on Windows (clean installed python):

>>> 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)>

@quattrozhou quattrozhou mannequin added type-crash A hard crash of the interpreter, possibly with a core dump 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes OS-windows labels Nov 28, 2021
@eryksun
Copy link
Contributor

eryksun commented Nov 28, 2021

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:

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.Pool examples will 
not work in the interactive interpreter. For example: ...

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.

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error and removed 3.7 (EOL) end of life 3.8 only security fixes type-crash A hard crash of the interpreter, possibly with a core dump labels Nov 28, 2021
@terryjreedy
Copy link
Member

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.

@terryjreedy terryjreedy added docs Documentation in the Doc dir 3.11 only security fixes and removed 3.9 only security fixes 3.10 only security fixes labels Dec 4, 2021
@terryjreedy terryjreedy added docs Documentation in the Doc dir 3.11 only security fixes labels Dec 4, 2021
@terryjreedy terryjreedy removed 3.9 only security fixes 3.10 only security fixes labels Dec 4, 2021
@eryksun
Copy link
Contributor

eryksun commented Dec 4, 2021

not quite clear to me it this only applies on Windows or is general.

macOS defaults to the spawn method (bpo-33725).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes docs Documentation in the Doc dir OS-windows topic-multiprocessing type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

4 participants