Skip to content
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

feat: use_execution_context hook #205

Merged
merged 3 commits into from
Jan 23, 2024
Merged
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
2 changes: 2 additions & 0 deletions plugins/ui/src/deephaven/ui/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .use_row_data import use_row_data
from .use_row_list import use_row_list
from .use_cell_data import use_cell_data
from .use_execution_context import use_execution_context


__all__ = [
Expand All @@ -23,4 +24,5 @@
"use_row_data",
"use_row_list",
"use_cell_data",
"use_execution_context",
]
36 changes: 36 additions & 0 deletions plugins/ui/src/deephaven/ui/hooks/use_execution_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from functools import partial
from typing import Callable

from deephaven.execution_context import get_exec_ctx, ExecutionContext

from . import use_memo


def func_with_ctx(
exec_ctx: ExecutionContext,
func: Callable,
) -> None:
"""
Call the function within an execution context.

Args:
exec_ctx: ExecutionContext: The execution context to use.
func: Callable: The function to call.
"""
with exec_ctx:
func()


def use_execution_context(exec_ctx: ExecutionContext = None) -> None:
"""
Create an execution context wrapper for a function.

Args:
exec_ctx: ExecutionContext: The execution context to use. Defaults to
the current execution context if not provided.
"""
exec_ctx = use_memo(lambda: exec_ctx if exec_ctx else get_exec_ctx(), [exec_ctx])
exec_fn = use_memo(lambda: partial(func_with_ctx, exec_ctx), [exec_ctx])
return exec_fn
48 changes: 48 additions & 0 deletions plugins/ui/test/deephaven/ui/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,54 @@ def _test_cell_data(t=table):

self.assertEqual(result, expected)

def test_execution_context(self):
from deephaven.ui.hooks import use_execution_context, use_state, use_memo
from deephaven import empty_table

def _test_execution_context():
with_exec_ctx = use_execution_context()

def table_func():
# This would fail if not using an execution context
empty_table(0).update("X=1")

def thread_func():
with_exec_ctx(table_func)

def start_thread():
thread = threading.Thread(target=thread_func)
thread.start()
thread.join()

use_memo(start_thread, [])

render_hook(_test_execution_context)

def test_execution_context_custom(self):
from deephaven.ui.hooks import use_execution_context, use_state, use_memo
from deephaven import empty_table
from deephaven.execution_context import make_user_exec_ctx

table = None

def _test_execution_context():
with_exec_ctx = use_execution_context(make_user_exec_ctx())

def table_func():
# This would fail if not using an execution context
empty_table(0).update("X=1")

def thread_func():
with_exec_ctx(table_func)

def start_thread():
thread = threading.Thread(target=thread_func)
thread.start()

use_memo(start_thread, [])

render_hook(_test_execution_context)


if __name__ == "__main__":
unittest.main()