python / cpython

The Python programming language
https://www.python.org
Other
62.44k stars 29.97k forks source link

Multi-processing example inaccurate warning #79072

Open 1badd512-0525-4080-8712-5291bd4c7493 opened 5 years ago

1badd512-0525-4080-8712-5291bd4c7493 commented 5 years ago
BPO 34891
Nosy @TonyFlury, @tirkarthi

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: ```python assignee = None closed_at = None created_at = labels = ['3.8', 'docs'] title = 'Multi-processing example inaccurate warning' updated_at = user = 'https://github.com/TonyFlury' ``` bugs.python.org fields: ```python activity = actor = 'anthony-flury' assignee = 'docs@python' closed = False closed_date = None closer = None components = ['Documentation'] creation = creator = 'anthony-flury' dependencies = [] files = [] hgrepos = [] issue_num = 34891 keywords = [] message_count = 3.0 messages = ['327046', '327049', '327238'] nosy_count = 3.0 nosy_names = ['docs@python', 'anthony-flury', 'xtreak'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue34891' versions = ['Python 3.8'] ```

1badd512-0525-4080-8712-5291bd4c7493 commented 5 years ago

On the Multi-processing page \https://docs.python.org/3/library/multiprocessing.html\ (just above the reference section) there is a warning that the examples wont work from the interpreter.

This is not entirely accurate in that the examples do work, in the interpreter within Linux (and Mac OS I think), where the O/S supports processes being forked.

In fact it is only in Windows, where a brand new process needs to be started and the source is effectively re-imported is there an issue running the examples in the interpreter.

Should the documentation make this clearer ?

tirkarthi commented 5 years ago

Thanks for the report. Are you referring to the below warning?

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:

I tried the example and it throws the AttributeError as shown in the example in Linux and MacOS. Maybe I am wrong on the above. Can you please add in the specific example that is shown as not executable from the interpreter but actually works?

Thanks

1badd512-0525-4080-8712-5291bd4c7493 commented 5 years ago

An example that does work :

    $ python3
  Python 3.6.6 (default, Sep 12 2018, 18:26:19) 
  [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
  Type "help", "copyright", "credits" or "license" for more information.
      >>> from multiprocessing import Pool
      >>> def f(x): 
      ...     return x**2
      ...
      >>> with Pool(10) as p:
      ...     print(p.map(f, range(1,10)))
      [1, 4, 9, 16, 25, 36, 49, 64, 81]

So something about having Pool as a context manager means it works - very odd then.

BTW - with the exception of the example saying 'don't do this it doesn't work', none of the examples on the page are shown on the command line interpreter; but the first example uses a Pool context manager - which works as above.