Issue 10128: multiprocessing.Pool throws exception with main.py (original) (raw)

Created on 2010-10-16 22:41 by Michael.Olson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue10128.patch Michael.Olson,2010-10-20 13:09 Patch for Issue 10128
Messages (13)
msg118905 - (view) Author: Michael Olson (Michael.Olson) Date: 2010-10-16 22:41
In an application with an entry point of __main__.py, multiprocessing.Pool throws the following: Traceback (most recent call last): File "", line 1, in File "D:\Dev\Python27\lib\multiprocessing\forking.py", line 346, in main prepare(preparation_data) File "D:\Dev\Python27\lib\multiprocessing\forking.py", line 454, in prepare assert main_name not in sys.modules, main_name AssertionError: __main__ These messages repeat as long as the application is running. Demonstration Code, must be in file named __main__.py: -------------------- import multiprocessing import time if __name__ == '__main__': pool = multiprocessing.Pool() time.sleep(2) --------------------
msg118969 - (view) Author: Michael Olson (Michael.Olson) Date: 2010-10-17 19:36
I wrapped the offending assertion in a if main_name != '__main__'. I considered not checking the module_name against built-in modules but that seemed likely to be the sort of thing being guarded against, so I left it at an exception for __main__. Ran a few trivial testing using Pool and Process and it seems to work fine.
msg119134 - (view) Author: Ask Solem (asksol) (Python committer) Date: 2010-10-19 12:36
Is this on Windows? Does it work for you now?
msg119186 - (view) Author: Michael Olson (Michael.Olson) Date: 2010-10-20 03:35
Sorry about that, yes, this is on Windows XP and 7, 32 bit. And with the if statement it seems to work fine. v/r -- Michael Olson
msg119204 - (view) Author: Michael Olson (Michael.Olson) Date: 2010-10-20 13:09
Ummm, I think I've been unclear on where I was making changes, I changed lib\multiprocessing\forking.py to fix the issue. Patch attached.
msg119205 - (view) Author: Michael Olson (Michael.Olson) Date: 2010-10-20 13:13
As a note, I didn't attach a patch at first because I was fairly sure I was kludging it into submission, but at least this makes it clear as to what I did. v/r -- Michael Olson
msg149989 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2011-12-21 14:22
The fix from #10845 should be backported to Python 2.7 (bypassing the assertion isn't enough - you want to skip re-executing __main__ entirely)
msg213269 - (view) Author: Martin Dengler (mdengler) * Date: 2014-03-12 16:12
I applied the patch from #10845 to 2.7.6 and it worked well for me.
msg236443 - (view) Author: Marc Schlaich (schlamar) * Date: 2015-02-23 13:55
Please fix this. Scripts with multiprocessing bundled as wheels are broken with Python 2.7 on Windows: https://github.com/pypa/pip/issues/1891
msg236485 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2015-02-24 10:06
Marc's reference to pip meant I noticed something that I had missed previously - that this issue is referring specifically to the use of setuptools/pip entry points, not to the -m switch. Entry points shouldn't be hitting this if they're importing the module containing the entry point normally - #10845 specifically related to invoking multiprocessing from a module run directly with the -m switch, which was worked around in 3.2 and 3.3, and then finally fixed properly in 3.4 by the implementation of #19946 So if there's an incompatibility between multiprocessing and entry points, it would be preferable to fix it in pip/setuptools, as that will reach many more installations than just fixing multiprocessing to better tolerate that situation.
msg254840 - (view) Author: Marc Schlaich (schlamar) * Date: 2015-11-18 08:39
Please see my latest comments to https://github.com/pypa/pip/issues/1891. tl;dr It is related to the -m switch as pip's wheel launcher does PYTHONPATH=script.exe python -m __main__
msg254872 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2015-11-19 01:53
Thanks for the investigation Marc. I'd been hesitant to backport the mitigation patch in #10845 to 2.7.x, (as it *does* represent a behavioural change), but if that code path is currently hitting an assert statement anyway, it seems reasonable to make the change for 2.7.11+
msg254875 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-11-19 02:59
New changeset 5d88c1d413b9 by Nick Coghlan in branch '2.7': Close #10128: don't rerun __main__.py in multiprocessing https://hg.python.org/cpython/rev/5d88c1d413b9
History
Date User Action Args
2022-04-11 14:57:07 admin set github: 54337
2015-11-19 02:59:57 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: resolved
2015-11-19 01:53:09 ncoghlan set messages: +
2015-11-18 08:39:47 schlamar set messages: +
2015-02-24 10:06:23 ncoghlan set nosy: + bkabrdamessages: +
2015-02-24 02:11:54 ned.deily set nosy: + sbt, davin
2015-02-23 13:55:54 schlamar set nosy: + schlamarmessages: +
2014-03-12 16:12:28 mdengler set messages: +
2014-03-12 16:11:23 mdengler set nosy: + mdengler
2011-12-21 14:22:03 ncoghlan set nosy: + ncoghlanmessages: +
2010-10-20 13:13:13 Michael.Olson set messages: +
2010-10-20 13:09:54 Michael.Olson set status: closed -> openfiles: + issue10128.patchmessages: + keywords: + patchresolution: not a bug -> (no value)
2010-10-20 07:20:48 asksol set status: open -> closedresolution: not a bug
2010-10-20 03:35:16 Michael.Olson set messages: +
2010-10-19 12:36:51 asksol set messages: +
2010-10-17 19:36:58 Michael.Olson set messages: +
2010-10-17 02:58:53 r.david.murray set nosy: + asksol
2010-10-17 02:26:07 ned.deily set nosy: + jnoller
2010-10-16 22:41:50 Michael.Olson create