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

Skip to content

gh-76327: freeze_support for non-Windows platforms #104607

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 3 additions & 12 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1015,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__ ==
Expand All @@ -1033,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()

Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions Lib/multiprocessing/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ 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.resource_tracker import main',
'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:]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix multiprocessing support for frozen executable on non-Windows platforms.
Remove outdated warning in document.
Loading