Set the default work start method to spawn on MacOS (#16089)

This commit is contained in:
Adrian Wälchli 2022-12-19 15:06:52 +01:00 committed by GitHub
parent 0fd3d54205
commit f3157f306a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 3 deletions

View File

@ -28,9 +28,13 @@ 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))
- The utility `lightning.app.utilities.cloud.is_running_in_cloud` now returns `True` during loading of the app locally when running with `--cloud` ([#16045](https://github.com/Lightning-AI/lightning/pull/16045))
### Deprecated
-

View File

@ -51,7 +51,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" if sys.platform in ("darwin", "win32") else "fork"
def __init__(
self,

View File

@ -0,0 +1,27 @@
from unittest import mock
from unittest.mock import MagicMock, Mock
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()

View File

@ -68,7 +68,7 @@ class ContextWork(LightningWork):
assert _get_context().value == "work"
class ContxtFlow(LightningFlow):
class ContextFlow(LightningFlow):
def __init__(self):
super().__init__()
self.work = ContextWork()
@ -83,7 +83,7 @@ class ContxtFlow(LightningFlow):
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(