Skip to content

Commit

Permalink
Dispatch bokeh events on threads
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 1, 2021
1 parent 31d37f6 commit 2164949
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
5 changes: 4 additions & 1 deletion panel/pane/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
data=json, data_sources=sources, events=self._selections,
throttle=self._throttle, **props
)
model.on_event('vega_event', self._process_event)
if comm:
model.on_event('vega_event', self._comm_event)
else:
model.on_event('vega_event', partial(self._server_event, doc))
if root is None:
root = model
self._models[root.ref['id']] = (model, parent)
Expand Down
35 changes: 25 additions & 10 deletions panel/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ def _change_coroutine(self, doc=None):
else:
self._change_event(doc)

@gen.coroutine
def _event_coroutine(self, event):
if state._thread_pool:
state._thread_pool.submit(self._process_event, event)
else:
self._process_event(event)

def _change_event(self, doc=None):
try:
state.curdoc = doc
Expand All @@ -309,11 +316,17 @@ def _comm_change(self, doc, ref, comm, subpath, attr, old, new):
else:
self._schedule_change(doc, comm)

def _comm_event(self, event):
if state._thread_pool:
state._thread_pool.submit(self._process_event, event)
else:
self._process_event(event)

def _server_event(self, doc, event):
state._locks.clear()
if doc.session_context:
doc.add_timeout_callback(
partial(self._process_event, event),
partial(self._event_coroutine, event),
self._debounce
)
else:
Expand All @@ -329,14 +342,16 @@ def _server_change(self, doc, ref, subpath, attr, old, new):
state._locks.clear()
processing = bool(self._events)
self._events.update({attr: new})
if not processing:
if doc.session_context:
doc.add_timeout_callback(
partial(self._change_coroutine, doc),
self._debounce
)
else:
self._change_event(doc)
if processing:
return

if doc.session_context:
doc.add_timeout_callback(
partial(self._change_coroutine, doc),
self._debounce
)
else:
self._change_event(doc)


class Reactive(Syncable, Viewable):
Expand Down Expand Up @@ -1532,7 +1547,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
model.children = self._get_children(doc, root, model, comm)

if comm:
model.on_event('dom_event', self._process_event)
model.on_event('dom_event', self._comm_event)
else:
model.on_event('dom_event', partial(self._server_event, doc))

Expand Down
2 changes: 1 addition & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
)
self._link_props(model, ['page', 'sorters', 'expanded', 'filters'], doc, root, comm)
if comm:
model.on_event('table-edit', self._process_event)
model.on_event('table-edit', self._comm_event)
else:
model.on_event('table-edit', partial(self._server_event, doc))
return model
Expand Down
2 changes: 1 addition & 1 deletion panel/widgets/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
model = super()._get_model(doc, root, parent, comm)
model.output = self.output
if comm:
model.on_event('keystroke', self._process_event)
model.on_event('keystroke', self._comm_event)
else:
model.on_event('keystroke', partial(self._server_event, doc))
return model
Expand Down

0 comments on commit 2164949

Please sign in to comment.