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 support for async functions on pn.state.add_periodic_callback #2737

Merged
merged 1 commit into from
Sep 15, 2021
Merged
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
11 changes: 8 additions & 3 deletions panel/io/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Defines callbacks to be executed on a thread or by scheduling it
on a running bokeh server.
"""
import asyncio
import inspect
import logging
import time

Expand Down Expand Up @@ -70,7 +72,7 @@ def _update_period(self):
self.stop()
self.start()

def _periodic_callback(self):
async def _periodic_callback(self):
with edit_readonly(state):
state.busy = True
cbname = function_name(self.callback)
Expand All @@ -79,7 +81,10 @@ def _periodic_callback(self):
LOG_PERIODIC_START, id(self._doc), cbname, self._counter
)
try:
self.callback()
if inspect.isasyncgenfunction(self.callback) or inspect.iscoroutinefunction(self.callback):
await self.callback()
else:
self.callback()
finally:
if self._doc:
_periodic_logger.info(
Expand Down Expand Up @@ -123,7 +128,7 @@ def start(self):
self._cb = self._doc.add_periodic_callback(self._periodic_callback, self.period)
else:
from tornado.ioloop import PeriodicCallback
self._cb = PeriodicCallback(self._periodic_callback, self.period)
self._cb = PeriodicCallback(lambda: asyncio.create_task(self._periodic_callback), self.period)
self._cb.start()
try:
state.on_session_destroyed(self._cleanup)
Expand Down