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

pn.state.add_periodic_callback does not support async functions #2736

Closed
MarcSkovMadsen opened this issue Sep 15, 2021 · 0 comments · Fixed by #2737
Closed

pn.state.add_periodic_callback does not support async functions #2736

MarcSkovMadsen opened this issue Sep 15, 2021 · 0 comments · Fixed by #2737
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Sep 15, 2021

Panel: 0.12.1.

I've created this on the back of Discourse - Running async periodic callback

According to Panel - Async Functions, pn.state.add_periodic_callback can run async functions.

But if I try I get

C:\Users\masma\Anaconda3\envs\aw-lib\lib\site-packages\panel\io\callbacks.py:72: RuntimeWarning: coroutine 'test' was never awaited   
  self.callback()
import panel as pn
import asyncio
pn.extension()

text = pn.widgets.StaticText(value="test")

async def test():
    print("halo")
    text.value = "halo"
    await asyncio.sleep(1)
    text.value = "halo2"

pn.state.add_periodic_callback(test, period=2000)
pn.Row(text).servable()

Workaround

According to tornadoweb/tornado#2828 (comment) I should use

pn.state.add_periodic_callback(lambda: asyncio.create_task(test()), period=2000)
async.mp4
import panel as pn
import asyncio
import datetime
pn.extension()

text = pn.widgets.StaticText(value="")

async def test():
    text.value = str(datetime.datetime.now()) + " start"
    await asyncio.sleep(1)
    text.value = str(datetime.datetime.now()) + " finish"

pn.state.add_periodic_callback(lambda: asyncio.create_task(test()), period=2000)
pn.Row(text).servable()

Solution

  • Maybe support async directly without the workaround
  • Document with a concrete example how to schedule async periodic callbacks.
@MarcSkovMadsen MarcSkovMadsen added TRIAGE Default label for untriaged issues type: bug Something isn't correct or isn't working and removed TRIAGE Default label for untriaged issues labels Sep 15, 2021
@MarcSkovMadsen MarcSkovMadsen added this to the 0.12.2 milestone Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant