From da124fdd8c824adf750c87ae30ea1c382e5e9ff9 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 19 Jun 2025 00:01:38 +0900 Subject: [PATCH] multiprocessing.get_context will not set the start method globally --- Lib/multiprocessing/context.py | 4 +--- Lib/test/_test_multiprocessing.py | 6 ++++++ .../Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index 051d567d457928..a7e319575832d0 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -236,9 +236,7 @@ def __init__(self, context): def get_context(self, method=None): if method is None: - if self._actual_context is None: - self._actual_context = self._default_context - return self._actual_context + return self._actual_context or self._default_context else: return super().get_context(method) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index a1259ff1d63d18..9b5ec8cf174bee 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5811,6 +5811,12 @@ def check_context(self, ctx): p.join() self.assertEqual(child_method, ctx.get_start_method()) + def test_default_context(self): + # Get_context should not have side effect, see gh-109070. + multiprocessing.set_start_method(None, force=True) + multiprocessing.get_context() + self.assertIsNone(multiprocessing.context._default_context._actual_context) + def test_context(self): for method in ('fork', 'spawn', 'forkserver'): try: diff --git a/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst b/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst new file mode 100644 index 00000000000000..e2f45736a438ce --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst @@ -0,0 +1 @@ +:func:`multiprocessing.get_context` will not set the start method globally. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy