]> granicus.if.org Git - python/commitdiff
Issue #12098: multiprocessing on Windows now starts child processes
authorKristján Valur Jónsson <sweskman@gmail.com>
Tue, 19 Mar 2013 22:07:35 +0000 (15:07 -0700)
committerKristján Valur Jónsson <sweskman@gmail.com>
Tue, 19 Mar 2013 22:07:35 +0000 (15:07 -0700)
  using the same sys.flags as the current process.
Backport from default branch.

Lib/multiprocessing/forking.py
Lib/multiprocessing/util.py
Lib/subprocess.py
Lib/test/test_multiprocessing.py
Lib/test/test_support.py
Misc/NEWS

index 1597ae8f2e3d36e4303c67483d38f651147380b5..dc465b4ed25da82ffda457a05e83ecbfcbcb7772 100644 (file)
@@ -361,7 +361,8 @@ else:
             return [sys.executable, '--multiprocessing-fork']
         else:
             prog = 'from multiprocessing.forking import main; main()'
-            return [_python_exe, '-c', prog, '--multiprocessing-fork']
+            opts = util._args_from_interpreter_flags()
+            return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork']
 
 
     def main():
index fe76bcca7ec85a7fd81add4544e82a92da956cc9..3ce480fc42002afb2bfbd048e90ff4b1c55d22da 100644 (file)
@@ -37,6 +37,7 @@ import weakref
 import atexit
 import threading        # we want threading to install it's
                         # cleanup function before multiprocessing does
+from subprocess import _args_from_interpreter_flags
 
 from multiprocessing.process import current_process, active_children
 
index 19a51889ea17f8f41ab41dcd84960163e64deef1..309f9a39728eddeb468d5c592cabe444f4d58556 100644 (file)
@@ -482,6 +482,37 @@ def _eintr_retry_call(func, *args):
             raise
 
 
+# XXX This function is only used by multiprocessing and the test suite,
+# but it's here so that it can be imported when Python is compiled without
+# threads.
+
+def _args_from_interpreter_flags():
+    """Return a list of command-line arguments reproducing the current
+    settings in sys.flags and sys.warnoptions."""
+    flag_opt_map = {
+        'debug': 'd',
+        # 'inspect': 'i',
+        # 'interactive': 'i',
+        'optimize': 'O',
+        'dont_write_bytecode': 'B',
+        'no_user_site': 's',
+        'no_site': 'S',
+        'ignore_environment': 'E',
+        'verbose': 'v',
+        'bytes_warning': 'b',
+        'hash_randomization': 'R',
+        'py3k_warning': '3',
+    }
+    args = []
+    for flag, opt in flag_opt_map.items():
+        v = getattr(sys.flags, flag)
+        if v > 0:
+            args.append('-' + opt * v)
+    for opt in sys.warnoptions:
+        args.append('-W' + opt)
+    return args
+
+
 def call(*popenargs, **kwargs):
     """Run command with arguments.  Wait for command to complete, then
     return the returncode attribute.
index 9b3f0a96473c6a6bc23eb9a0b8401e5d7be6cc01..8091a9f95f8cae8010fc5375626c3775ccccbd9f 100644 (file)
@@ -2400,11 +2400,43 @@ class TestNoForkBomb(unittest.TestCase):
             self.assertEqual('', err.decode('ascii'))
 
 #
+# Issue 12098: check sys.flags of child matches that for parent
+#
+
+class TestFlags(unittest.TestCase):
+    @classmethod
+    def run_in_grandchild(cls, conn):
+        conn.send(tuple(sys.flags))
+
+    @classmethod
+    def run_in_child(cls):
+        import json
+        r, w = multiprocessing.Pipe(duplex=False)
+        p = multiprocessing.Process(target=cls.run_in_grandchild, args=(w,))
+        p.start()
+        grandchild_flags = r.recv()
+        p.join()
+        r.close()
+        w.close()
+        flags = (tuple(sys.flags), grandchild_flags)
+        print(json.dumps(flags))
+
+    def test_flags(self):
+        import json, subprocess
+        # start child process using unusual flags
+        prog = ('from test.test_multiprocessing import TestFlags; ' +
+                'TestFlags.run_in_child()')
+        data = subprocess.check_output(
+            [sys.executable, '-E', '-S', '-O', '-c', prog])
+        child_flags, grandchild_flags = json.loads(data.decode('ascii'))
+        self.assertEqual(child_flags, grandchild_flags)
+#
 #
 #
 
 testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
-                   TestStdinBadfiledescriptor, TestTimeouts, TestNoForkBomb]
+                   TestStdinBadfiledescriptor, TestTimeouts, TestNoForkBomb,
+                   TestFlags]
 
 #
 #
index 034a374bd65e2498331e975fa1fb781b10537d66..b3076c1cfd44d17fd4e1753993b566b5d329757e 100644 (file)
@@ -1344,22 +1344,7 @@ def py3k_bytes(b):
 def args_from_interpreter_flags():
     """Return a list of command-line arguments reproducing the current
     settings in sys.flags."""
-    flag_opt_map = {
-        'bytes_warning': 'b',
-        'dont_write_bytecode': 'B',
-        'ignore_environment': 'E',
-        'no_user_site': 's',
-        'no_site': 'S',
-        'optimize': 'O',
-        'py3k_warning': '3',
-        'verbose': 'v',
-    }
-    args = []
-    for flag, opt in flag_opt_map.items():
-        v = getattr(sys.flags, flag)
-        if v > 0:
-            args.append('-' + opt * v)
-    return args
+    return subprocess._args_from_interpreter_flags()
 
 def strip_python_stderr(stderr):
     """Strip the stderr of a Python process from potential debug output
index 67c1e9e376a96206d061a5bfad2a7140f1610969..f309e6f3d2339d9272981d5d24f37021ff0b7d72 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -214,6 +214,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #12098: multiprocessing on Windows now starts child processes
+  using the same sys.flags as the current process.  Initial patch by
+  Sergey Mezentsev.
+
 - Issue #8862: Fixed curses cleanup when getkey is interrputed by a signal.
 
 - Issue #9090: When a socket with a timeout fails with EWOULDBLOCK or EAGAIN,