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

Fix task scheduling in pyodide #3511

Merged
merged 3 commits into from
May 11, 2022
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
10 changes: 3 additions & 7 deletions panel/io/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,10 @@ async def _async_repeat(self, func):
"""
while True:
start = time.monotonic()
self._cb = asyncio.ensure_future(func())
await self._cb
await func()
timeout = (self.period/1000.) - (time.monotonic()-start)
if timeout > 0:
self._cb = asyncio.ensure_future(asyncio.sleep(timeout))
await self._cb
await asyncio.sleep(timeout)

def _cleanup(self, session_context):
self.stop()
Expand All @@ -165,11 +163,9 @@ def start(self):
self._updating = False
self._start_time = time.time()
if state._is_pyodide:
event_loop = asyncio.get_running_loop()
task = asyncio.create_task(
self._cb = asyncio.create_task(
self._async_repeat(self._periodic_callback)
)
event_loop.call_soon(task)
elif state.curdoc:
self._doc = state.curdoc
self._cb = self._doc.add_periodic_callback(self._periodic_callback, self.period)
Expand Down
4 changes: 2 additions & 2 deletions panel/io/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def async_execute(func):
event_loop = asyncio.get_running_loop()
if event_loop.is_running():
event_loop.call_soon(func)
asyncio.create_task(func())
else:
event_loop.run_until_complete(func())
return
Expand Down Expand Up @@ -124,7 +124,7 @@ async def show(obj, target):
"""
from js import console
console.log('panel.io.pyodide.show is deprecated in favor of panel.io.pyodide.write')
write(target, obj)
await write(target, obj)

async def write(target, obj):
"""
Expand Down
2 changes: 1 addition & 1 deletion panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def servable(
out = sys.stdout._out
else:
raise ValueError("Could not determine target node to write to.")
param.parameterized.async_executor(asyncio.create_task(write(out, self)))
asyncio.create_task(write(out, self))
return self

def show(
Expand Down