10000 gh-135726: multiprocessing.freeze_support no longer sets the start method globally. by aisk · Pull Request #135810 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-135726: multiprocessing.freeze_support no longer sets the start method globally. #135810

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ def get_all_start_methods(self):
)
return start_method_names

def freeze_support(self):
'''Check whether this is a fake forked process in a frozen executable.
If so then run code specified by commandline and exit.
'''
start_method = self.get_start_method(allow_none=True)
if start_method is None:
start_method = self._default_context.get_start_method()
if start_method == 'spawn' and getattr(sys, 'frozen', False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding this method to DefaultContext, which complicates the picture of which one is called when and why two are defined in the heirarchy, perhaps edit the one in BaseContext to something like this:

diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py
index 051d567d457..2d5c9a1d3dd 100644
--- a/Lib/multiprocessing/context.py
+++ b/Lib/multiprocessing/context.py
@@ -145,7 +145,11 @@ def freeze_support(self):
         '''Check whether this is a fake forked process in a frozen executable.
         If so then run code specified by commandline and exit.
         '''
-        if self.get_start_method() == 'spawn' and getattr(sys, 'frozen', False):
+        # GH-135726: allow_none=True prevents this "get" call from setting the
+        # default context as the selected one as a side-effect.
+        if (getattr(sys, 'frozen', False) and
+            isinstance(self.get_start_method(allow_none=True)
+                       or _default_context, SpawnContext)):
             from .spawn import freeze_support
             freeze_support()
 

(untested - I'm just trying to reason though the logic)

from .spawn import freeze_support
freeze_support()


#
# Context types for fixed start method
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`multiprocessing.freeze_support` no longer sets the start method
globally.
Loading
0