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

Conversation

jnumainville
Copy link
Collaborator

@jnumainville jnumainville commented Jan 12, 2024

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

@jnumainville jnumainville requested a review from mofojed January 12, 2024 15:50
Comment on lines 34 to 36
exec_ctx = use_ref(exec_ctx if exec_ctx else get_exec_ctx())

return partial(func_with_ctx, exec_ctx.current)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to memoize this instead:

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

@jnumainville jnumainville merged commit 76cd7ab into deephaven:main Jan 23, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hook to use execution context
2 participants