Skip to content
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

bpo-46720: Add support for path-like objects to multiprocessing.set_executable for Windows to be on a par with Unix-like systems #31279

Merged
merged 18 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,9 @@ Miscellaneous
.. versionchanged:: 3.4
Now supported on Unix when the ``'spawn'`` start method is used.

.. versionchanged:: 3.11
Accepts a :term:`path-like object`.

.. function:: set_start_method(method)

Set the method which should be used to start child processes.
Expand Down
18 changes: 11 additions & 7 deletions Lib/multiprocessing/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@
WINEXE = getattr(sys, 'frozen', False)
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")

if WINSERVICE:
_python_exe = os.path.join(sys.exec_prefix, 'python.exe')
else:
_python_exe = sys.executable

def set_executable(exe):
global _python_exe
_python_exe = exe
if sys.platform == 'win32':
_python_exe = os.fsdecode(exe)
else:
_python_exe = os.fsencode(exe)
geryogam marked this conversation as resolved.
Show resolved Hide resolved

def get_executable():
return _python_exe

if WINSERVICE:
set_executable(os.path.join(sys.exec_prefix, 'python.exe'))
else:
set_executable(sys.executable)

#
#
#
Expand Down Expand Up @@ -86,7 +89,8 @@ def get_command_line(**kwds):
prog = 'from multiprocessing.spawn import spawn_main; spawn_main(%s)'
prog %= ', '.join('%s=%r' % item for item in kwds.items())
opts = util._args_from_interpreter_flags()
return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork']
exe = get_executable()
return [exe] + opts + ['-c', prog, '--multiprocessing-fork']


def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None):
Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def spawnv_passfds(path, args, passfds):
errpipe_read, errpipe_write = os.pipe()
try:
return _posixsubprocess.fork_exec(
args, [os.fsencode(path)], True, passfds, None, None,
args, [path], True, passfds, None, None,
-1, -1, -1, -1, -1, -1, errpipe_read, errpipe_write,
False, False, None, None, None, -1, None)
finally:
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import subprocess
import struct
import operator
import pathlib
import pickle
import weakref
import warnings
Expand Down Expand Up @@ -253,6 +254,21 @@ def test_current(self):
self.assertEqual(current.ident, os.getpid())
self.assertEqual(current.exitcode, None)

def test_set_executable(self):
if self.TYPE == 'threads':
self.skipTest(f'test not appropriate for {self.TYPE}')
paths = [
sys.executable, # str
sys.executable.encode(), # bytes
pathlib.Path(sys.executable) # os.PathLike
]
for path in paths:
self.set_executable(path)
p = self.Process()
p.start()
p.join()
self.assertEqual(p.exitcode, 0)

def test_args_argument(self):
# bpo-45735: Using list or tuple as *args* in constructor could
# achieve the same effect.
Expand Down Expand Up @@ -5779,6 +5795,7 @@ class ProcessesMixin(BaseMixin):
current_process = staticmethod(multiprocessing.current_process)
parent_process = staticmethod(multiprocessing.parent_process)
active_children = staticmethod(multiprocessing.active_children)
set_executable = staticmethod(multiprocessing.set_executable)
Pool = staticmethod(multiprocessing.Pool)
Pipe = staticmethod(multiprocessing.Pipe)
Queue = staticmethod(multiprocessing.Queue)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add support for path-like objects to :func:`multiprocessing.set_executable` for
Windows to be on a par with Unix-like systems. Patch by Géry Ogam.