diff options
author | Gregory P. Smith <greg@krypto.org> | 2023-02-02 23:50:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-02 23:50:35 (GMT) |
commit | 0ca67e6313c11263ecaef7ce182308eeb5aa6814 (patch) | |
tree | ff302df77417456ee4dcf94082b6d942e320b13a /Lib/multiprocessing | |
parent | 618b7a8260bb40290d6551f24885931077309590 (diff) | |
download | cpython-0ca67e6313c11263ecaef7ce182308eeb5aa6814.zip cpython-0ca67e6313c11263ecaef7ce182308eeb5aa6814.tar.gz cpython-0ca67e6313c11263ecaef7ce182308eeb5aa6814.tar.bz2 |
GH-84559: Deprecate fork being the multiprocessing default. (#100618)
This starts the process. Users who don't specify their own start method
and use the default on platforms where it is 'fork' will see a
DeprecationWarning upon multiprocessing.Pool() construction or upon
multiprocessing.Process.start() or concurrent.futures.ProcessPool use.
See the related issue and documentation within this change for details.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r-- | Lib/multiprocessing/context.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index b1960ea..010a920 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -23,6 +23,9 @@ class TimeoutError(ProcessError): class AuthenticationError(ProcessError): pass +class DefaultForkDeprecationWarning(DeprecationWarning): + pass + # # Base type for contexts. Bound methods of an instance of this type are included in __all__ of __init__.py # @@ -258,6 +261,7 @@ class DefaultContext(BaseContext): return self._actual_context._name def get_all_start_methods(self): + """Returns a list of the supported start methods, default first.""" if sys.platform == 'win32': return ['spawn'] else: @@ -280,6 +284,23 @@ if sys.platform != 'win32': from .popen_fork import Popen return Popen(process_obj) + _warn_package_prefixes = (os.path.dirname(__file__),) + + class _DeprecatedForkProcess(ForkProcess): + @classmethod + def _Popen(cls, process_obj): + import warnings + warnings.warn( + "The default multiprocessing start method will change " + "away from 'fork' in Python >= 3.14, per GH-84559. " + "Use multiprocessing.get_context(X) or .set_start_method(X) to " + "explicitly specify it when your application requires 'fork'. " + "The safest start method is 'spawn'.", + category=DefaultForkDeprecationWarning, + skip_file_prefixes=_warn_package_prefixes, + ) + return super()._Popen(process_obj) + class SpawnProcess(process.BaseProcess): _start_method = 'spawn' @staticmethod @@ -303,6 +324,9 @@ if sys.platform != 'win32': _name = 'fork' Process = ForkProcess + class _DefaultForkContext(ForkContext): + Process = _DeprecatedForkProcess + class SpawnContext(BaseContext): _name = 'spawn' Process = SpawnProcess @@ -318,13 +342,16 @@ if sys.platform != 'win32': 'fork': ForkContext(), 'spawn': SpawnContext(), 'forkserver': ForkServerContext(), + # Remove None and _DefaultForkContext() when changing the default + # in 3.14 for https://github.com/python/cpython/issues/84559. + None: _DefaultForkContext(), } if sys.platform == 'darwin': # bpo-33725: running arbitrary code after fork() is no longer reliable # on macOS since macOS 10.14 (Mojave). Use spawn by default instead. _default_context = DefaultContext(_concrete_contexts['spawn']) else: - _default_context = DefaultContext(_concrete_contexts['fork']) + _default_context = DefaultContext(_concrete_contexts[None]) else: |