Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77c84f2

Browse files
committed
#12098: Make multiprocessing's child processes inherit sys.flags on Windows
Initial patch by Sergey Mezentsev.
1 parent cca802e commit 77c84f2

5 files changed

Lines changed: 73 additions & 20 deletions

File tree

Lib/multiprocessing/forking.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ def get_command_line():
324324
return [sys.executable, '--multiprocessing-fork']
325325
else:
326326
prog = 'from multiprocessing.forking import main; main()'
327-
return [_python_exe, '-c', prog, '--multiprocessing-fork']
327+
opts = util._args_from_interpreter_flags()
328+
return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork']
328329

329330

330331
def main():

Lib/multiprocessing/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Licensed to PSF under a Contributor Agreement.
88
#
99

10+
import sys
1011
import functools
1112
import itertools
1213
import weakref
@@ -295,3 +296,33 @@ def __init__(self):
295296
register_after_fork(self, lambda obj : obj.__dict__.clear())
296297
def __reduce__(self):
297298
return type(self), ()
299+
300+
#
301+
# Get options for python to produce the same sys.flags
302+
#
303+
304+
def _args_from_interpreter_flags():
305+
"""Return a list of command-line arguments reproducing the current
306+
settings in sys.flags and sys.warnoptions."""
307+
flag_opt_map = {
308+
'debug': 'd',
309+
# 'inspect': 'i',
310+
# 'interactive': 'i',
311+
'optimize': 'O',
312+
'dont_write_bytecode': 'B',
313+
'no_user_site': 's',
314+
'no_site': 'S',
315+
'ignore_environment': 'E',
316+
'verbose': 'v',
317+
'bytes_warning': 'b',
318+
'quiet': 'q',
319+
'hash_randomization': 'R',
320+
}
321+
args = []
322+
for flag, opt in flag_opt_map.items():
323+
v = getattr(sys.flags, flag)
324+
if v > 0:
325+
args.append('-' + opt * v)
326+
for opt in sys.warnoptions:
327+
args.append('-W' + opt)
328+
return args

Lib/test/support.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,24 +1596,8 @@ def strip_python_stderr(stderr):
15961596
def args_from_interpreter_flags():
15971597
"""Return a list of command-line arguments reproducing the current
15981598
settings in sys.flags and sys.warnoptions."""
1599-
flag_opt_map = {
1600-
'bytes_warning': 'b',
1601-
'dont_write_bytecode': 'B',
1602-
'hash_randomization': 'R',
1603-
'ignore_environment': 'E',
1604-
'no_user_site': 's',
1605-
'no_site': 'S',
1606-
'optimize': 'O',
1607-
'verbose': 'v',
1608-
}
1609-
args = []
1610-
for flag, opt in flag_opt_map.items():
1611-
v = getattr(sys.flags, flag)
1612-
if v > 0:
1613-
args.append('-' + opt * v)
1614-
for opt in sys.warnoptions:
1615-
args.append('-W' + opt)
1616-
return args
1599+
from multiprocessing.util import _args_from_interpreter_flags
1600+
return _args_from_interpreter_flags()
16171601

16181602
#============================================================
16191603
# Support for assertions about logging.

Lib/test/test_multiprocessing.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,8 +2814,41 @@ def test_invalid_family_win32(self):
28142814
with self.assertRaises(ValueError):
28152815
multiprocessing.connection.Listener('/var/test.pipe')
28162816

2817+
#
2818+
# Issue 12098: check sys.flags of child matches that for parent
2819+
#
2820+
2821+
class TestFlags(unittest.TestCase):
2822+
@classmethod
2823+
def run_in_grandchild(cls, conn):
2824+
conn.send(tuple(sys.flags))
2825+
2826+
@classmethod
2827+
def run_in_child(cls):
2828+
import json
2829+
r, w = multiprocessing.Pipe(duplex=False)
2830+
p = multiprocessing.Process(target=cls.run_in_grandchild, args=(w,))
2831+
p.start()
2832+
grandchild_flags = r.recv()
2833+
p.join()
2834+
r.close()
2835+
w.close()
2836+
flags = (tuple(sys.flags), grandchild_flags)
2837+
print(json.dumps(flags))
2838+
2839+
def test_flags(self):
2840+
import json, subprocess
2841+
# start child process using unusual flags
2842+
prog = ('from test.test_multiprocessing import TestFlags; ' +
2843+
'TestFlags.run_in_child()')
2844+
data = subprocess.check_output(
2845+
[sys.executable, '-E', '-S', '-O', '-c', prog])
2846+
child_flags, grandchild_flags = json.loads(data.decode('ascii'))
2847+
self.assertEqual(child_flags, grandchild_flags)
2848+
28172849
testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
2818-
TestStdinBadfiledescriptor, TestWait, TestInvalidFamily]
2850+
TestStdinBadfiledescriptor, TestWait, TestInvalidFamily,
2851+
TestFlags]
28192852

28202853
#
28212854
#

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #12098: multiprocessing on Windows now starts child processes
42+
using the same sys.flags as the current process. Initial patch by
43+
Sergey Mezentsev.
44+
4145
- Issue #13031: Small speed-up for tarfile when unzipping tarfiles.
4246
Patch by Justin Peel.
4347

0 commit comments

Comments
 (0)