From 079b9aadfc8e55583bcecf6baba9b338869b5dff Mon Sep 17 00:00:00 2001 From: Zetaspace Date: Mon, 15 May 2023 17:31:18 +0800 Subject: [PATCH 1/5] gh-76327: freeze_support for non-Windows platforms This was exactly the same patch from [PyInstaller](https://github.com/pyinstaller/pyinstaller/blob/develop/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py#L26). --- Lib/multiprocessing/context.py | 2 +- Lib/multiprocessing/spawn.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index de8a264829dff3..df06aaf52a8edc 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -145,7 +145,7 @@ def freeze_support(self): '''Check whether this is a fake forked process in a frozen executable. If so then run code specified by commandline and exit. ''' - if sys.platform == 'win32' and getattr(sys, 'frozen', False): + if getattr(sys, 'frozen', False): from .spawn import freeze_support freeze_support() diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py index 09f8a229d7cccb..e131afda9ce776 100644 --- a/Lib/multiprocessing/spawn.py +++ b/Lib/multiprocessing/spawn.py @@ -66,6 +66,16 @@ def freeze_support(): ''' Run code for process object if this in not the main process ''' + if ( + len(sys.argv) >= 2 and sys.argv[-2] == '-c' and sys.argv[-1].startswith(( + 'from multiprocessing.semaphore_tracker import main', # Py<3.8 + 'from multiprocessing.resource_tracker import main', # Py>=3.8 + 'from multiprocessing.forkserver import main' + )) and set(sys.argv[1:-2]) == set(util._args_from_interpreter_flags()) + ): + exec(sys.argv[-1]) + sys.exit() + if is_forking(sys.argv): kwds = {} for arg in sys.argv[2:]: From af20c808b3bd5ba5036bdf5428b6afafa4c1da98 Mon Sep 17 00:00:00 2001 From: Zetaspace Date: Thu, 18 May 2023 12:12:41 +0800 Subject: [PATCH 2/5] gh-76327: Remove warnings about frozen executables on Unix --- Doc/library/multiprocessing.rst | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 8454296b815b41..af143e8abce0bc 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -216,13 +216,6 @@ A library which wants to use a particular start method should probably use :func:`get_context` to avoid interfering with the choice of the library user. -.. warning:: - - The ``'spawn'`` and ``'forkserver'`` start methods generally cannot - be used with "frozen" executables (i.e., binaries produced by - packages like **PyInstaller** and **cx_Freeze**) on POSIX systems. - The ``'fork'`` start method may work if code does not use threads. - Exchanging objects between processes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From b065b97e1a50d92387def0ba07ff7c9430b967cd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 18 May 2023 09:43:03 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-05-18-09-43-02.gh-issue-76327.tHxKwf.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-18-09-43-02.gh-issue-76327.tHxKwf.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-18-09-43-02.gh-issue-76327.tHxKwf.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-18-09-43-02.gh-issue-76327.tHxKwf.rst new file mode 100644 index 00000000000000..126ec176c5a7bc --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-18-09-43-02.gh-issue-76327.tHxKwf.rst @@ -0,0 +1,2 @@ +Fix multiprocessing support for frozen executable on non-Windows platforms. +Remove outdated warning in document. From fbf8e6b40367b6d44bcd291c76a9c620a8c7ba46 Mon Sep 17 00:00:00 2001 From: Zetaspace Date: Fri, 19 May 2023 12:07:31 +0800 Subject: [PATCH 4/5] Fix document about freeze_support --- Doc/library/multiprocessing.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index af143e8abce0bc..fece0b83cace01 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -1008,7 +1008,7 @@ Miscellaneous .. function:: freeze_support() Add support for when a program which uses :mod:`multiprocessing` has been - frozen to produce a Windows executable. (Has been tested with **py2exe**, + frozen to produce an executable. (Has been tested with **py2exe**, **PyInstaller** and **cx_Freeze**.) One needs to call this function straight after the ``if __name__ == @@ -1026,10 +1026,8 @@ Miscellaneous If the ``freeze_support()`` line is omitted then trying to run the frozen executable will raise :exc:`RuntimeError`. - Calling ``freeze_support()`` has no effect when invoked on any operating - system other than Windows. In addition, if the module is being run - normally by the Python interpreter on Windows (the program has not been - frozen), then ``freeze_support()`` has no effect. + If the module is being run normally by the Python interpreter (the program + has not been frozen), then ``freeze_support()`` has no effect. .. function:: get_all_start_methods() From 7dcc80be839fbd55410fb002d4967e010adff8f6 Mon Sep 17 00:00:00 2001 From: zetaloop <36418285+zetaloop@users.noreply.github.com> Date: Tue, 30 May 2023 10:00:26 +0800 Subject: [PATCH 5/5] Remove older version support. --- Lib/multiprocessing/spawn.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py index e131afda9ce776..fdc8597ad05d41 100644 --- a/Lib/multiprocessing/spawn.py +++ b/Lib/multiprocessing/spawn.py @@ -68,8 +68,7 @@ def freeze_support(): ''' if ( len(sys.argv) >= 2 and sys.argv[-2] == '-c' and sys.argv[-1].startswith(( - 'from multiprocessing.semaphore_tracker import main', # Py<3.8 - 'from multiprocessing.resource_tracker import main', # Py>=3.8 + 'from multiprocessing.resource_tracker import main', 'from multiprocessing.forkserver import main' )) and set(sys.argv[1:-2]) == set(util._args_from_interpreter_flags()) ):