From cd55d1dad1aeddf52f3fa8804a1cf4dc68602b6c Mon Sep 17 00:00:00 2001 From: awaelchli Date: Fri, 16 Dec 2022 12:21:58 +0100 Subject: [PATCH 1/7] set the default work start method to spawn --- src/lightning_app/components/serve/gradio.py | 2 -- src/lightning_app/components/serve/python_server.py | 2 -- src/lightning_app/core/work.py | 3 +-- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/lightning_app/components/serve/gradio.py b/src/lightning_app/components/serve/gradio.py index 7c07129d39b25..a1c8a2967b3eb 100644 --- a/src/lightning_app/components/serve/gradio.py +++ b/src/lightning_app/components/serve/gradio.py @@ -34,8 +34,6 @@ class ServeGradio(LightningWork, abc.ABC): title: Optional[str] = None description: Optional[str] = None - _start_method = "spawn" - def __init__(self, *args, **kwargs): requires("gradio")(super().__init__(*args, **kwargs)) assert self.inputs diff --git a/src/lightning_app/components/serve/python_server.py b/src/lightning_app/components/serve/python_server.py index 40b7e83a3bdca..327b05856dd4d 100644 --- a/src/lightning_app/components/serve/python_server.py +++ b/src/lightning_app/components/serve/python_server.py @@ -65,8 +65,6 @@ def _get_sample_data() -> Dict[Any, Any]: class PythonServer(LightningWork, abc.ABC): - _start_method = "spawn" - @requires(["torch"]) def __init__( # type: ignore self, diff --git a/src/lightning_app/core/work.py b/src/lightning_app/core/work.py index 43ffc0006d5ea..a9da87d20d69c 100644 --- a/src/lightning_app/core/work.py +++ b/src/lightning_app/core/work.py @@ -55,8 +55,7 @@ class LightningWork: ) _run_executor_cls: Type[WorkRunExecutor] = WorkRunExecutor - # TODO: Move to spawn for all Operating System. - _start_method = "spawn" if sys.platform == "win32" else "fork" + _start_method = "spawn" def __init__( self, From adceb497ec17afa3e81439b354e1ce8784864cab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Dec 2022 11:24:44 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/lightning_app/components/serve/python_server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lightning_app/components/serve/python_server.py b/src/lightning_app/components/serve/python_server.py index 327b05856dd4d..2bfc921f682cb 100644 --- a/src/lightning_app/components/serve/python_server.py +++ b/src/lightning_app/components/serve/python_server.py @@ -64,7 +64,6 @@ def _get_sample_data() -> Dict[Any, Any]: class PythonServer(LightningWork, abc.ABC): - @requires(["torch"]) def __init__( # type: ignore self, From efd25587c1bc3fc43b734fbd6aa331c1ab1fab2e Mon Sep 17 00:00:00 2001 From: awaelchli Date: Sun, 18 Dec 2022 22:53:52 +0100 Subject: [PATCH 3/7] fix typo --- tests/tests_app/runners/test_multiprocess.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests_app/runners/test_multiprocess.py b/tests/tests_app/runners/test_multiprocess.py index 2e1a34ab38677..48bbedf555d63 100644 --- a/tests/tests_app/runners/test_multiprocess.py +++ b/tests/tests_app/runners/test_multiprocess.py @@ -68,7 +68,7 @@ def run(self): assert _get_context().value == "work" -class ContxtFlow(LightningFlow): +class ContextFlow(LightningFlow): def __init__(self): super().__init__() self.work = ContextWork() @@ -83,7 +83,7 @@ def run(self): def test_multiprocess_runtime_sets_context(): """Test that the runtime sets the global variable COMPONENT_CONTEXT in Flow and Work.""" - MultiProcessRuntime(LightningApp(ContxtFlow())).dispatch() + MultiProcessRuntime(LightningApp(ContextFlow())).dispatch() @pytest.mark.parametrize( From 1e10d79e702a8e8dc27a41c83a589bad584a5ea6 Mon Sep 17 00:00:00 2001 From: awaelchli Date: Sun, 18 Dec 2022 22:56:44 +0100 Subject: [PATCH 4/7] only for darwin --- src/lightning_app/components/serve/gradio.py | 2 ++ src/lightning_app/components/serve/python_server.py | 3 +++ src/lightning_app/core/work.py | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lightning_app/components/serve/gradio.py b/src/lightning_app/components/serve/gradio.py index a1c8a2967b3eb..7c07129d39b25 100644 --- a/src/lightning_app/components/serve/gradio.py +++ b/src/lightning_app/components/serve/gradio.py @@ -34,6 +34,8 @@ class ServeGradio(LightningWork, abc.ABC): title: Optional[str] = None description: Optional[str] = None + _start_method = "spawn" + def __init__(self, *args, **kwargs): requires("gradio")(super().__init__(*args, **kwargs)) assert self.inputs diff --git a/src/lightning_app/components/serve/python_server.py b/src/lightning_app/components/serve/python_server.py index 1d40d5f9d1998..ee958b30625fd 100644 --- a/src/lightning_app/components/serve/python_server.py +++ b/src/lightning_app/components/serve/python_server.py @@ -129,6 +129,9 @@ def get_sample_data() -> Dict[Any, Any]: class PythonServer(LightningWork, abc.ABC): + + _start_method = "spawn" + @requires(["torch"]) def __init__( # type: ignore self, diff --git a/src/lightning_app/core/work.py b/src/lightning_app/core/work.py index 8b72fbef53118..c5cb52ca71a89 100644 --- a/src/lightning_app/core/work.py +++ b/src/lightning_app/core/work.py @@ -50,7 +50,8 @@ class LightningWork: ) _run_executor_cls: Type[WorkRunExecutor] = WorkRunExecutor - _start_method = "spawn" + # TODO: Move to spawn for all Operating System. + _start_method = "spawn" if sys.platform in ("darwin", "win32") else "fork" def __init__( self, From 44a69ca287d355e4808ecdd1300cf18b7cec87a6 Mon Sep 17 00:00:00 2001 From: awaelchli Date: Mon, 19 Dec 2022 01:28:27 +0100 Subject: [PATCH 5/7] add test --- tests/tests_app/runners/backends/__init__.py | 0 .../runners/backends/test_mp_process.py | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/tests_app/runners/backends/__init__.py create mode 100644 tests/tests_app/runners/backends/test_mp_process.py diff --git a/tests/tests_app/runners/backends/__init__.py b/tests/tests_app/runners/backends/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/tests_app/runners/backends/test_mp_process.py b/tests/tests_app/runners/backends/test_mp_process.py new file mode 100644 index 0000000000000..967d2aa2d6612 --- /dev/null +++ b/tests/tests_app/runners/backends/test_mp_process.py @@ -0,0 +1,27 @@ +from unittest import mock +from unittest.mock import Mock, MagicMock + +from lightning_app import LightningApp, LightningWork +from lightning_app.runners.backends import MultiProcessingBackend + + +@mock.patch("lightning_app.runners.backends.mp_process.multiprocessing") +def test_backend_create_work_with_set_start_method(multiprocessing_mock): + backend = MultiProcessingBackend(entrypoint_file="fake.py") + work = Mock(spec=LightningWork) + work._start_method = "test_start_method" + + app = LightningApp(work) + app.caller_queues = MagicMock() + app.delta_queue = MagicMock() + app.readiness_queue = MagicMock() + app.error_queue = MagicMock() + app.request_queues = MagicMock() + app.response_queues = MagicMock() + app.copy_request_queues = MagicMock() + app.copy_response_queues = MagicMock() + app.flow_to_work_delta_queues = MagicMock() + + backend.create_work(app=app, work=work) + multiprocessing_mock.get_context.assert_called_with("test_start_method") + multiprocessing_mock.get_context().Process().start.assert_called_once() From 95b5d7d1e0c8256ecac2ff4f43f339c6364e6814 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 19 Dec 2022 00:29:54 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/tests_app/runners/backends/test_mp_process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests_app/runners/backends/test_mp_process.py b/tests/tests_app/runners/backends/test_mp_process.py index 967d2aa2d6612..49a926b6832d6 100644 --- a/tests/tests_app/runners/backends/test_mp_process.py +++ b/tests/tests_app/runners/backends/test_mp_process.py @@ -1,5 +1,5 @@ from unittest import mock -from unittest.mock import Mock, MagicMock +from unittest.mock import MagicMock, Mock from lightning_app import LightningApp, LightningWork from lightning_app.runners.backends import MultiProcessingBackend From de2454e8abd4d61a3cf31826bee517084ce96742 Mon Sep 17 00:00:00 2001 From: awaelchli Date: Mon, 19 Dec 2022 01:31:54 +0100 Subject: [PATCH 7/7] changelog --- src/lightning_app/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index 1ffd4f1efb55f..fc2b47e0b58a0 100644 --- a/src/lightning_app/CHANGELOG.md +++ b/src/lightning_app/CHANGELOG.md @@ -21,7 +21,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Changed -- +- The default `start_method` for creating Work processes locally on MacOS is now 'spawn' (previously 'fork') ([#16089](https://github.com/Lightning-AI/lightning/pull/16089)) ### Deprecated