-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use_execution_context hook (#205)
The hook can be used to call code within an execution context It is a fairly niche hook, but here as an example of it being used to do a table update on a separate thread ``` import deephaven.ui as ui import threading from deephaven.execution_context import get_exec_ctx from deephaven import agg as agg import deephaven.pandas as dhpd import deephaven.plot.express as dx stocks = dx.data.stocks() def print_sym(source): print(dhpd.to_pandas(source.update(["Symbol = sym"]) \ .agg_by([agg.avg(cols=["AVG = size"])], by=["Symbol"]))) @ui.component def exec_ctx_hook(source): with_exec_ctx = ui.use_execution_context() def thread_func(): #print_sym(source) with_exec_ctx(lambda: print_sym(source)) def start_thread(): thread = threading.Thread(target=thread_func) thread.start() ui.use_memo(start_thread, source) return None exec_hook = exec_ctx_hook(stocks) ``` Fixes #184
- Loading branch information
1 parent
63945e7
commit 76cd7ab
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
plugins/ui/src/deephaven/ui/hooks/use_execution_context.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters