Skip to content

Commit

Permalink
Set Kedro Viz start method to spawn process while launching it from J…
Browse files Browse the repository at this point in the history
…upyter Notebook (#1696)

* add start method

Signed-off-by: ravi-kumar-pilla <[email protected]>

* modify set start method to use context

Signed-off-by: ravi-kumar-pilla <[email protected]>

* update pytest

Signed-off-by: ravi-kumar-pilla <[email protected]>

* add type ignore

Signed-off-by: ravi-kumar-pilla <[email protected]>

* update release note

Signed-off-by: ravi-kumar-pilla <[email protected]>

* add mypy ignore for entire file

Signed-off-by: ravi-kumar-pilla <[email protected]>

---------

Signed-off-by: ravi-kumar-pilla <[email protected]>
  • Loading branch information
ravi-kumar-pilla authored Jan 12, 2024
1 parent 7213c7e commit 92836db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 6 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Please follow the established format:
- Use present tense (e.g. 'Add new feature')
- Include the ID number for the related PR (or PRs) in parentheses
-->
# Upcoming Release

## Bug fixes and other changes

- Set Kedro Viz start method to spawn process while launching it from Jupyter Notebook. (#1696)

# Release 7.0.0

## Major features and improvements
Expand Down
1 change: 1 addition & 0 deletions package/kedro_viz/api/graphql/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""`kedro_viz.api.graphql.router` defines GraphQL routes."""
# mypy: ignore-errors
from fastapi import APIRouter
from strawberry.asgi import GraphQL

Expand Down
3 changes: 2 additions & 1 deletion package/kedro_viz/launchers/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def run_viz(port: int = None, local_ns: Dict[str, Any] = None) -> None:
else None
)

viz_process = multiprocessing.Process(
process_context = multiprocessing.get_context("spawn")
viz_process = process_context.Process(
target=run_server,
daemon=True,
kwargs={"project_path": project_path, "host": host, "port": port},
Expand Down
31 changes: 22 additions & 9 deletions package/tests/test_launchers/test_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ def patched_check_viz_up(mocker):

class TestRunVizLineMagic:
def test_run_viz(self, mocker, patched_check_viz_up):
process_init = mocker.patch("multiprocessing.Process")
jupyter_display = mocker.patch("kedro_viz.launchers.jupyter.display")
mock_process_context = mocker.patch("multiprocessing.get_context")
mock_context_instance = mocker.Mock()

mock_process_context.return_value = mock_context_instance
mock_process = mocker.patch.object(mock_context_instance, "Process")
mock_jupyter_display = mocker.patch("kedro_viz.launchers.jupyter.display")

run_viz()
process_init.assert_called_once_with(

mock_process_context.assert_called_once_with("spawn")
mock_process.assert_called_once_with(
target=run_server,
daemon=True,
kwargs={
Expand All @@ -24,13 +31,15 @@ def test_run_viz(self, mocker, patched_check_viz_up):
"port": 4141,
},
)
jupyter_display.assert_called_once()
mock_jupyter_display.assert_called_once()
assert set(_VIZ_PROCESSES.keys()) == {4141}

# call run_viz another time should reuse the same port
process_init.reset_mock()
mock_process.reset_mock()

run_viz()
process_init.assert_called_once_with(

mock_process.assert_called_once_with(
target=run_server,
daemon=True,
kwargs={
Expand Down Expand Up @@ -58,14 +67,18 @@ def test_exception_when_viz_cannot_be_launched(self, mocker):

def test_run_viz_on_databricks(self, mocker, patched_check_viz_up, monkeypatch):
monkeypatch.setenv("DATABRICKS_RUNTIME_VERSION", "1")
process_init = mocker.patch("multiprocessing.Process")
mock_process_context = mocker.patch("multiprocessing.get_context")
mock_context_instance = mocker.Mock()

mock_process_context.return_value = mock_context_instance
mock_process = mocker.patch.object(mock_context_instance, "Process")
mocker.patch("kedro_viz.launchers.jupyter._is_databricks", return_value=True)
databricks_display = mocker.patch(
"kedro_viz.launchers.jupyter._display_databricks_html"
)
process_init.reset_mock()
mock_process.reset_mock()
run_viz()
process_init.assert_called_once_with(
mock_process.assert_called_once_with(
target=run_server,
daemon=True,
kwargs={
Expand Down

0 comments on commit 92836db

Please sign in to comment.