Skip to content

Commit

Permalink
improve docs, formatting and cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte committed Jun 11, 2024
1 parent 9865409 commit d99f2b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
33 changes: 20 additions & 13 deletions doc/src/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,18 @@ sources.
Multiprocessing support
-----------------------

On Linux, macOS, and Windows, multiprocessing support is managed by cx_Freeze,
including support for pyTorch.
On Linux, macOS, and Windows, :pythondocs:`multiprocessing
<library/multiprocessing.html>` support is managed by **cx_Freeze**,
including support for PyTorch.

Depending on the platform, multiprocessing supports three ways to start a
process. These start methods are: spawn, fork, and forkserver.

However, to produce an executable, you must use
`multiprocessing.freeze_support()`.

One needs to call this function straight after the if __name__ == '__main__'
line of the main module. For example:
One needs to call this function straight after the
``if __name__ == "__main__"`` line of the main module. For example:

.. code-block:: python
Expand All @@ -228,13 +232,16 @@ line of the main module. For example:
freeze_support()
Process(target=f).start()
If the freeze_support() line is omitted, then running the frozen executable
If the `freeze_support()` line is omitted, then running the frozen executable
will raise RuntimeError on Windows. On Linux and macOS a similar message is
shown but cx_Freeze tries to run the program by injecting a freeze_support.

Contrary to what the Python docs may state, you MUST use
`multiprocessing.freeze_support()` on Linux, macOS, and Windows. On Linux and
macOS, cx_Freeze patches the call to also handle
`multiprocessing.spawn.freeze_support()` when needed. In addition, if the
module is being run normally by the Python interpreter on any OS
(the program has not been frozen), then freeze_support() has no effect.
shown but cx_Freeze tries to run the program by injecting a `freeze_support`.
In addition, if the module is being run normally by the Python interpreter on
any OS (the program has not been frozen), then `freeze_support()` has no
effect.

.. note::

Contrary to what the Python docs may state, you MUST use
`multiprocessing.freeze_support()` on Linux, macOS, and Windows.
On Linux and macOS, cx_Freeze patches the call to also handle
`multiprocessing.spawn.freeze_support()` when needed.
20 changes: 12 additions & 8 deletions tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

SOURCE = """\
sample1.py
import multiprocessing, sys
import multiprocessing
def foo(q):
q.put('hello')
q.put("Hello from cx_Freeze")
if __name__ == '__main__':
if __name__ == "__main__":
multiprocessing.freeze_support()
multiprocessing.set_start_method('spawn')
q = multiprocessing.SimpleQueue()
Expand All @@ -35,12 +35,12 @@ def foo(q):
print(q.get())
p.join()
sample2.py
import multiprocessing, sys
import multiprocessing
def foo(q):
q.put('hello')
q.put("Hello from cx_Freeze")
if __name__ == '__main__':
if __name__ == "__main__":
ctx = multiprocessing.get_context('spawn')
ctx.freeze_support()
q = ctx.Queue()
Expand All @@ -49,7 +49,7 @@ def foo(q):
print(q.get())
p.join()
sample3.py
if __name__ == "__main__":
if __name__ == "__main__":
import multiprocessing, sys
multiprocessing.freeze_support()
multiprocessing.set_start_method('spawn')
Expand Down Expand Up @@ -77,7 +77,11 @@ def foo(q):
}
)
"""
EXPECTED_OUTPUT = ["hello", "hello", "creating dict...done!"]
EXPECTED_OUTPUT = [
"Hello from cx_Freeze",
"Hello from cx_Freeze",
"creating dict...done!",
]


def _parameters_data() -> Iterator:
Expand Down

0 comments on commit d99f2b9

Please sign in to comment.