-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Queue render state updates on a thread (#182)
- Create a thread and queue for applying state updates - Now callbacks and state updates are all queued on the same thread - Cleaned up some typing in the `Renderer` class - Added a `use_render_queue` hook and example for showing how to queue state updates - Fixes #116 - Tested using the snippet from #116: ```python import deephaven.ui as ui from deephaven.ui import use_state @ui.component def foo(): a, set_a = use_state(0) b, set_b = use_state(0) print(f"Render with a {a} and b {b}") def handle_press(): set_a(a + 1) set_b(b + 1) return ui.action_button( f"a is {a} and b is {b}", on_press=handle_press ) f = foo() ``` - Ensured that the print out was only printed once per press of the button, and both values stayed the same - Allow a callable to be passed into the use_state setter function that takes the old value as a parameter. Tested using the following component: ```python import deephaven.ui as ui from deephaven.ui import use_state @ui.component def bar(): x, set_x = use_state(0) print(f"Render with x {x}") def handle_press(): # Call set_x twice in the same method, using a callable. This should result in x increasing by 2 each time the button is pressed set_x(lambda old_x: old_x + 1) set_x(lambda old_x: old_x + 1) return ui.action_button( f"x is {x}", on_press=handle_press ) b = bar() ``` - Tested that trying to update state from the wrong thread throws an error: ```python import deephaven.ui as ui import threading import time from deephaven.ui import use_state @ui.component def foo(): a, set_a = use_state(0) b, set_b = use_state(0) print(f"Render with a {a} and b {b}") def handle_press(): def update_state(): time.sleep(1) set_a(a + 1) set_b(b + 1) # Not using the correct thread threading.Thread(target=update_state).start() return ui.action_button( f"a is {a} and b is {b}", on_press=handle_press ) f = foo() ```
- Loading branch information
Showing
16 changed files
with
589 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 |
---|---|---|
@@ -1,28 +1,27 @@ | ||
from .RenderContext import RenderContext | ||
from typing import Optional | ||
import threading | ||
|
||
|
||
class UiSharedInternals: | ||
""" | ||
Shared internal context for the deephaven.ui plugin to use when rendering. | ||
Should be set at the start of a render call, and unset at the end. | ||
TODO: Need to keep track of context for each given thread, in case we have multiple threads rendering at once. | ||
""" | ||
|
||
_current_context: Optional[RenderContext] = None | ||
class NoContextException(Exception): | ||
pass | ||
|
||
@property | ||
def current_context(self) -> RenderContext: | ||
return self._current_context | ||
|
||
|
||
_deephaven_ui_shared_internals: UiSharedInternals = UiSharedInternals() | ||
_local_data = threading.local() | ||
|
||
|
||
def get_context() -> RenderContext: | ||
return _deephaven_ui_shared_internals.current_context | ||
try: | ||
return _local_data.context | ||
except AttributeError: | ||
raise NoContextException("No context set") | ||
|
||
|
||
def set_context(context): | ||
_deephaven_ui_shared_internals._current_context = context | ||
def set_context(context: Optional[RenderContext]): | ||
""" | ||
Set the current context for the thread. Can be set to None to unset the context for a thread | ||
""" | ||
if context is None: | ||
del _local_data.context | ||
else: | ||
_local_data.context = context |
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
Oops, something went wrong.