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

ReactiveHTML Param Watcher: Coroutine never awaited. #7142

Closed
emcd opened this issue Aug 13, 2024 · 2 comments
Closed

ReactiveHTML Param Watcher: Coroutine never awaited. #7142

emcd opened this issue Aug 13, 2024 · 2 comments

Comments

@emcd
Copy link

emcd commented Aug 13, 2024

Probably a case not covered by #3262 or holoviz/param#913.

ALL software version info

$ hatch run python -V
Python 3.10.14
$ hatch run pip freeze | grep -E '^(bokeh|panel|param|tornado)'
bokeh==3.4.3
panel==1.4.5
param==2.1.1
tornado==6.4.1

Description of expected behavior and the observed behavior

Expected behavior (which is seen with synchronous event handler):

$ hatch run python bug-reports/panel-rehtml-async-bug.py
...
Event(what='value', name='clicked', obj=Div(clicked=True), cls=Div(clicked=True), old=False, new=True, type='changed')

Observed behavior (which is seen with coroutine event handler):

$ hatch run python bug-reports/panel-rehtml-async-bug.py
...
/home/me/.local/share/hatch/env/virtual/aiwb/WoaaKFKL/aiwb/lib/python3.10/site-packages/param/parameterized.py:2506: RuntimeWarning: coroutine 'on_div_clicked' was never awaited
  watcher.fn(*args, **kwargs)

Complete, minimal, self-contained example code that reproduces the issue

import param

from panel.reactive import ReactiveHTML


class Div( ReactiveHTML ):

    clicked = param.Event( default = False )

    _template = '''<div id="Div" onclick="${_div_click}">Testing...</div>'''

    def _div_click( self, event ):
        self.clicked = True


#def on_div_clicked( event ): # Works
async def on_div_clicked( event ): # Broken
    print( event )


div = Div( )
div.param.watch( lambda event: on_div_clicked( event ), 'clicked' )


div.show( )
@philippjfr
Copy link
Member

This is a bug in your code, not Panel itself. Specifically you cannot call an asynchronous function in your lambda without awaiting it. It works just fine if you simply do:

div.param.watch(on_div_clicked, 'clicked' )

@emcd
Copy link
Author

emcd commented Aug 27, 2024

Oops... forgot to replace the lambda when converting from sync to async. Not obvious from the above reproducer, but I use lambdas in my actual code to drop the event argument, since I usually don't care about it, and to access a closure cell instead, which carries the information that I do care about. Sorry for the noise. I'll switch to using partial functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants